I have a simple program:
int main(){
int *p[2];
p[0] = new int;
p[1] = new int;
*p[0] = 10;
*p[1] = 12;
delete p[0];
delete p[1];
}
Compile:
g++ test.cpp -g -fno-stack-protector -o test
Using GDB to debug (break before variable destruction):
(gdb) x p
0x7fffffffdfc0: 0x5556aed0
(gdb) x &p
0x7fffffffdfc0: 0x5556aed0
(gdb) x p+1
0x7fffffffdfc8: 0x5556aef0
(gdb) x &p+1
0x7fffffffdfd0: 0xffffe0d0
So my question is:
- Why
pis same with&p?I thinkpmeans the start address of array, namely&p[0], while&pstores the address of array. I thought they share no relationship. - Then as
pequals&p, what defines the behavior for+1,whyp+1is&p[1], but&p+1is next address afterint *p[2].