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.
main
. – Cessionmain
. In C,main
can be called as a regular function, so it needs to receive arguments like a regular function and must obey the ABI. – Betoken%zu
– Pearcysizeof
tounsigned
and then format with%u
. – Filature