Assume we have a simple C++ code as the following:
#include <iostream>
int main(){
  int a = 5;
}
Since each memory location is 8 bits and an integer is 32 bits I assume the memory structure for a would be like this:
0xa      0xb      0xc      0xd 
00000000 00000000 00000000 00000101
where 0xa,0xb,0xc,0xd are sample memory addresses.
1) is &a pointing to 0xa or 0xd? 
2) if I use GDB and and use x to get real memory addresses I get the following:
(gdb) p a
$7 = 5
(gdb) p &a
$8 = (int *) 0x7ffeefbffac8
(gdb) x/bt 0x7ffeefbffac8
0x7ffeefbffac8: 00000101
(gdb) x/bt 0x7ffeefbffac8-1
0x7ffeefbffac7: 00000000
(gdb) x/bt 0x7ffeefbffac8-2
0x7ffeefbffac6: 00000000
(gdb) x/bt 0x7ffeefbffac8-3
0x7ffeefbffac5: 01111111
(gdb) 
why is 0x7ffeefbffac8-3 populated with 01111111 and not 00000000? ins't this address equal to 0xa in our sample memory address?
On a little-endian machine, &a points to the least significant byte of memory. That is, if &a == 0x7ffeefbffac8, then a resides in bytes
0x7ffeefbffac8:  101   << least significant byte
0x7ffeefbffac9:  000
0x7ffeefbffaca:  000
0x7ffeefbffacb:  000   << most significant byte.
This is best observed by assigning e.g. 0x0a090705 to a, and then:
Temporary breakpoint 1, main (argc=3, argv=0x7fffffffdc68) at t.c:2
2     int a = 0x0a090705;
(gdb) n
3     return 0;
(gdb) p &a
$1 = (int *) 0x7fffffffdb7c
Examine 4 bytes starting from &a:
(gdb) x/4bt 0x7fffffffdb7c
0x7fffffffdb7c: 00000101    00000111    00001001    00001010
Or, equivalently, do so one byte at a time:
(gdb) x/bt 0x7fffffffdb7c
0x7fffffffdb7c: 00000101
(gdb) x/bt 0x7fffffffdb7c+1
0x7fffffffdb7d: 00000111
(gdb) x/bt 0x7fffffffdb7c+2
0x7fffffffdb7e: 00001001
(gdb) x/bt 0x7fffffffdb7c+3
0x7fffffffdb7f: 00001010
why is
0x7ffeefbffac8-3populated with01111111and not00000000?
Because you are going in the wrong direction: &a-3 isn't part of a at all (it's part of something else, or possibly uninitialized random garbage).
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