I ran the following program on my computer (64-bit Intel running Linux).
#include <stdio.h> void test(int argc, char **argv) { printf("[test] Argc Pointer: %p\n", &argc); printf("[test] Argv Pointer: %p\n", &argv); } int main(int argc, char **argv) { printf("Argc Pointer: %p\n", &argc); printf("Argv Pointer: %p\n", &argv); printf("Size of &argc: %lu\n", sizeof (&argc)); printf("Size of &argv: %lu\n", sizeof (&argv)); test(argc, argv); return 0; }
The output of the program was
$ gcc size.c -o size $ ./size Argc Pointer: 0x7fffd7000e4c Argv Pointer: 0x7fffd7000e40 Size of &argc: 8 Size of &argv: 8 [test] Argc Pointer: 0x7fffd7000e2c [test] Argv Pointer: 0x7fffd7000e20
The size of the pointer &argv
is 8 bytes. I expected the address of argc
to be address of (argv) + sizeof (argv) = 0x7ffed1a4c9f0 + 0x8 = 0x7ffed1a4c9f8
but there is a 4 byte padding in between them. Why is this the case?
My guess is that it could be due to memory alignment, but I am not sure.
I notice the same behaviour with the functions I call as well.
He said the memory size of argv in int main(int argc, char **argv) is 48 bytes, including itself.
The first parameter, argc (argument count) is an integer that indicates how many arguments were entered on the command line when the program was started. The second parameter, argv (argument vector), is an array of pointers to arrays of character objects.
— argv[argc] shall be a null pointer. The rationale for this is to provide a redundant check for the end of the argument list, on the basis of common practice (ref: Rationale for the ANSI C programming language (1990), 2.1.
argc stands for argument count and argv stands for argument values. These are variables passed to the main function when it starts executing. When we run a program we can give arguments to that program like − $ ./a.out hello.
On your system, the first few integer or pointer arguments are passed in registers and have no addresses. When you take their addresses with &argc
or &argv
, the compiler has to fabricate addresses by writing the register contents to stack locations and giving you the addresses of those stack locations. In doing so, the compiler chooses, in a sense, whatever stack locations happen to be convenient for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With