So I have a C program. And I don't think I can post any code snippets due to complexity issues. But I'll outline my error, because it's weird, and see if anyone can give any insights.
I set a pointer to NULL
. If, in the same function where I set the pointer to NULL
, I printf()
the pointer (with "%p"
), I get 0x0
, and when I print that same pointer a million miles away at the end of my program, I get 0x0
. If I remove the printf()
and make absolutely no other changes, then when the pointer is printed later, I get 0x1
, and other random variables in my structure have incorrect values as well. I'm compiling it with GCC on -O2
, but it has the same behavior if I take off optimization, so that's not hte problem.
This sounds like a Heisenbug, and I have no idea why it's happening, nor how to fix it. Does anyone who has dealt with something like this in the past have advice on how they approached this kind of problem? I know this may sound kind of vague.
EDIT: Somehow, it works now. Thank you, all of you, for your suggestions.
The debugger told me interesting things - that my variable was getting optimized away. So I rewrote the function so it didn't need the intermediate variable, and now it works with and without the printf()
. I have a vague idea of what might have been happening, but I need sleep more than I need to know what was happening.