I am debugging a C code where I have a pointer inside a pointer to a struct named board
. There is a function where I am printing the board:
static void board_print(board *b){
int i,j;
char data;
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
data = b->data[i * size + j];
if(data){
printf("X ");
}else{
printf("O ");
}
}
printf("\n");
}
}
Here is the weird part. When I hit my breakpoint initially at the beginning of first for loop, everything is OK, my data is correct, all the pointers work etc. as seen below:
Then, I step into the loop for the first time, with i and j equal to zero, and b->data[0]
should be perfectly valid, as it was two steps ago. Suddenly, as soon as I step on the line data = b->data[i * size + j];
, the data pointer changes into a null pointer. When I execute the line, I (obviously) get a bad access error, as seen below:
What could be the reason? I've used C before, and I've pretty got a grasp of it, but I've never seen a pointer value changing suddenly to null before while stepping in a single-threaded simple C program. I am using Apple LLVM Compiler 4.1 to compile and lldb to debug, which are the defaults with XCode 4.5.
Update: The same behavior observed with compiling with gcc and debugging with gdb. Almost hundred percent an arror at my side, but I have no idea what is wrong with the code..
Update #2: I've noticed something even stranger on gcc/gdb now. The just before executing the line data = b->data[i * size + j];
, I can access everything from the debugger with no issues. Right after executing that line, I can't access b->data
entirely, including the values that I've accessed right before stepping:
After the $4 = ...
line which executed successfully in debugger, I've stepped over the line. Then I've got various addressing errors as see above. I really have no idea what's going on...
Update #3: I've noticed something very weird. Here, first look at the fix that I've implemented. This one started to work with no problem when I got rid of the variable named data
completely:
Now, look closely at the screenshot that I've uploaded with the Update #2: Right after I assign a value to the local variable named data
, also b->data
gets its address changed. It looks like a side effect of the assignment. But I have no idea what is the reason behind it.
Obviously something / somebody changes the property "data" of the board structure.
Why ? I can only see 3 reasons :
Your application is multi-threaded and an other thread updates the data pointer to NULL (which is not your case, you are using only one thread, sorry didn't notice).
The board structure is allocated from the stack but the content is not any more valid... For example : The pointer of a local variable is returned, but the variable (the struct) is destroyed (because this is a local variable) when returning the pointer...
The board structure is allocated from the heap then freed, and finally this freed pointer is still used : the heap memory is corrupted by something else...
My guess : The second point which is a frequent mistake
So a simple question : The board struct is stored in HEAP or STACK memory ?