watch #define constant expression in debug

Guest #5126946
Rate this post
• ▲ useful
▼ not useful
Hello guys,
i came about a something strange to me while programming and Debugging 
some c-Code with AVR-Studio 7. I was trying to watch the value of an 
Expression while Debugging, but i get the message "unknown identifier". 
The Expression is defined as follows:

#define THRESH1 (uint16_t) ((uint32_t)1024*(500 + 1*800)/5000)

does anyone has an idea, why i can't watch the value of this constant? 
By the way, i've tried declaring it localy or global, but i still get 
the same message. Moreover, i tried with the following Expression:

const uint16_t thresh1_3 = (uint16_t) ((uint32_t)1024*(500 + 
3*800)/5000);

but the watch variable only works when it is a local constant, not 
global. Does anyone has a hint there too? I also deactivated 
optimizations too but it doesn't seem to help. Thanks and Cheers
Paul
Guest
Rate this post
• ▲ useful
▼ not useful
Paul wrote:
> Hello guys,
> i came about a something strange to me while programming and Debugging
> some c-Code with AVR-Studio 7. I was trying to watch the value of an
> Expression while Debugging, but i get the message "unknown identifier".
> The Expression is defined as follows:
>
> #define THRESH1 (uint16_t) ((uint32_t)1024*(500 + 1*800)/5000)
>
> does anyone has an idea, why i can't watch the value of this constant?

Because it is a preprocessor macro. At runtime, it doesn't exist in that 
form anymore. A macro is similar to your editors search/replace 
functions, just with the difference that the text to be replaced is 
specified within the processed file itself.

> By the way, i've tried declaring it localy or global, but i still get
> the same message.

There is no such thing as "locally" or "gobally" with respect to macros.

> Moreover, i tried with the following Expression:
>
> const uint16_t thresh1_3 = (uint16_t) ((uint32_t)1024*(500 +
> 3*800)/5000);
>
> but the watch variable only works when it is a local constant, not
> global.

It might get optimized out by the compiler. Does the symbol thresh1_3 
exist in the generated executable?
Rate this post
• ▲ useful
▼ not useful
> Because it is a preprocessor macro. At runtime, it doesn't exist in that
> form anymore. A macro is similar to your editors search/replace
> functions, just with the difference that the text to be replaced is
> specified within the processed file itself.

Thank you, that is what i tought too, anyways I want to use the 
calculated constant to test for a certain condition. So I expected I 
could actually see what value was calculated:
1
#define THRESH1_1 (uint16_t) ((uint32_t)1024*(500 + 1*800)/5000)
2
#define THRESH1_2 (uint16_t) ((uint32_t)1024*(500 + 2*800)/5000)
3
#define THRESH1_3 (uint16_t) ((uint32_t)1024*(500 + 3*800)/5000)
4
#define THRESH1_4 (uint16_t) ((uint32_t)1024*(500 + 4*800)/5000)
5
if(u16_adValue0 <= THRESH1_1)
6
{
7
... something;
8
}  
9
else if(u16_adValue0 <= THRESH1_2) 
10
{
11
... some other thing;
12
}
and so on...

> It might get optimized out by the compiler. Does the symbol thresh1_3
> exist in the generated executable?
I did deactivate optimizations, but it didn't Change anything. I don't 
really understand what you mean with generated executable because i'm 
flashing the Code directly in a microcontroller (ATmega32u4)
Guest
Rate this post
• ▲ useful
▼ not useful
If your macro is expected to represent just a numerical expression 
(macros could actually represent anything) then you could directly 
assign it to a variable for debugging purposes. If you make that 
variable volatile then it won't be optimized away and you can watch it 
in the debugger to confirm that your macro expands and evaluates to the 
correct value.
1
#define FOO_BAR  (uint16_t) ((uint32_t)1024*(500 + 1*800)/5000)
2

3
// fixme: remove this after you know its evaluated correctly
4
volatile uint16_t debug_foo_bar = FOO_BAR;

Reply

A question of your own? This thread is over 9 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.