cancel
Showing results for 
Search instead for 
Did you mean: 

One of my pointer values is getting optimized out. How do I prevent this?

matt2399
Associate II
Posted on December 23, 2014 at 21:53

I'm stepping through my code to see where a certain bug occurs with my display driver, and found that something strange is going on. An instance of the class gets initialized properly, but once I try to use a method of that class, the pointer value seems to have been changed somehow.

I appear to initialize correctly...

Display * volatile const sys_disp = new Display();

(gdb) p sys_disp

$1 = (Display * const volatile) 0x20002638

...but the value gets changed once I want to to use it.

sys_disp->write_all_lines();

(gdb) p sys_disp

$2 = (Display * const volatile) 0x0

Any ideas as to how I can keep the value the way it was? Why is this happening? I'll be happy to provide more debug output if necessary.

#debug #volatile #optimizations
1 REPLY 1
Danish1
Lead II
Posted on December 24, 2014 at 13:13

As far as I know, to be ''optimised out'' means that the compiler has decided that the variable is not needed long-term, so it does not need to be stored in memory.

First question:

Is this a global variable (i.e. outside the scope of all functions)?

Otherwise, unless it is declared static, it will not retain its value between function calls.

Second question:

How strongly do you know that your code is not clobbering the variable by e.g. writing beyond the end of a buffer that happens to be nearby in memory?

Can you put a breakpoint on all writes to that memory location? (I don't know how you'd do that in gdb; I use Rowley Crossworks which has that ability).

Hope this helps,

Danish