void myFunc(char dummy) {
char *addrFirstArg = &dummy;
}
int main() {
char dummy = 42;
myFunc(dummy);
return 0;
}
I run the above under gdb and add a breakpoint at myFunc. I step once to compute the addrFirstArg value and examine it.
I also do
info frameto spit out information about the frame myFunc. As far as my understanding of the C stack implementation goes, I expect that addrFirstArg should be 8 bytes above the base pointer for the frame myFunc.
This is the output that I see:
(gdb) p &dummy
$1 = 0xffffd094 "*\202\f\b\032\004"
(gdb) info frame
Stack level 0, frame at 0xffffd0b0:
eip = 0x8048330 in findStackBottom (reporter.c:64); saved eip 0x8048478
called by frame at 0xffffd170
source language c.
Arglist at 0xffffd0a8, args: dummy=42 '*'
Locals at 0xffffd0a8, Previous frame's sp is 0xffffd0b0
Saved registers:
ebp at 0xffffd0a8, eip at 0xffffd0ac
(gdb) x/1c 0xffffd0b0
0xffffd0b0: 42 'a'
Thus, inside the frame myFunc, ebp points to the location 0xffffd0a8, where as the address of dummy is 0xffffd094, which is 0x14 bytes below ebp, instead of being 0x8 bytes above it.
This 'discrepancy' disappears if I declare my dummy to be an int and myFunc to take in an int argument.
I'm really intrigued by this behavior. It was reproducible - I ran it a bunch of times.