Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exact Memory Size of argv in C

Tags:

c

argv

So my professor gave me (where x is a C executable):

$ ./x y z w

He said the memory size of argv in int main(int argc, char **argv) is 48 bytes, including itself.

Can someone help explain this to me?

like image 429
yking Avatar asked Oct 22 '25 03:10

yking


1 Answers

argv is, as the function says, a pointer to a pointer to a char, which in terms of size means it's a pointer. Generally (almost always but not guaranteed?), all pointers are the same size. If this is a 64-bit machine, pointers will be 8 bytes in size. In this case, argv contains five elements: pointers to five char pointers: './x', 'y', 'z', 'w', and NULL, since argv is NULL-terminated.

By my calculations, that leaves 40 bytes in argv, so I'm not sure where he's getting the other 8.

Edit: As someone else suggested, argv itself would also take up room for a pointer, so there's the other 8.

like image 97
Dan Fego Avatar answered Oct 23 '25 19:10

Dan Fego



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!