This question is about C, pointers and gcc. My program writes the number 10 in 5 positions adjacent to the address pointed by the variable num. Apparently those 10's are recorded correctly but only for a while. When I do the second FOR and try to see the values of the previously recorded positions I do not see the 10's I recorded. Why? Who is erasing those 10's? The garbage collector?
Code
void main( void ) {
int num = 10;
int *pnum = #
for( int i=0; i < 5; i++ ) {
pnum = pnum - 1;
*pnum = 10;
printf( "\n%p, %d", pnum, *pnum );
}
puts( "===========================" );
int *p = #
for( int i = 0; i < 5; i++ ) {
p = p - 1;
printf( "\n%p, %d", p, *p );
}
}
Output
0xffffcbec, 10
0xffffcbe8, 10
0xffffcbe4, 10
0xffffcbe0, 10
0xffffcbdc, 10
============================= 0xffffcbec, 1
0xffffcbe8, -2146187872
0xffffcbe4, 0
0xffffcbe0, 0
0xffffcbdc, 0