Reputation: 1416
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.
Upvotes: 41
Views: 3328
Reputation: 181689
Why are the addresses of argc and argv 12 bytes apart?
From the perspective of the language standard, the answer is "no particular reason". C does not specify or imply any relationship between the addresses of function parameters. @EricPostpischil describes what is probably happening in your particular implementation, but those details would be different for an implementation in which all arguments are passed on the stack, and that is not the only alternative.
Moreover, I'm having trouble coming up with a way in which such information could be useful within a program. For example, even if you "know" that the address of argv
is 12 bytes before the address of argc
, there's still no defined way to compute one of those pointers from the other.
Upvotes: 11
Reputation: 224312
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.
Upvotes: 61