I am trying to preserve a variable so I can see its value while debugging optimized code. Why is the following an illegal constant expression?
void foo(uint_32 x)
{
static uint_32 y = x;
...
}
For your purpose you probably want this:
void foo(uint_32 x)
{
static uint_32 y;
y = x;
...
}
What you tried to do is an initialisation. What is done above is an assignment.
Maybe for your purpose this would be even more interesting:
static uint_32 y;
void foo(uint_32 x)
{
y = x;
...
}
Now the variable y
can easily be accessed by the debugger once the foo
function is finished.
"Why is the following an illegal constant expression?"
Because static
variables have to be initialized with a value known at compile-time, while x
is only determined at run-time.
Note that this use of static
is meant for to keeping the variable with its stored value between different calls to foo()
alive (existing in memory) - Means the object won´t get destroyed/ deallocated after one single execution of the function, as it is the case with function-local variables of the storage class auto
matic.
It wouldn´t make sense to create and initialize a static
variable at each function call new.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With