const address

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

const address

1,258 Views
Caius
Contributor I
Hi,
 
I'm using codewarrior but I get a "illegal constant expression" when declaring something like:
 
u08* const ptr = (u08* const)  ( (u32)&array[16] & ~0xF );
 
It seems to me that the above expression be evaluated before runtime.
Can anybody help?
 
Thanks in advance.
Labels (1)
0 Kudos
Reply
2 Replies

437 Views
admin
Specialist II
The legality of this declaration depends on storage duration of the variable ptr. If this variable has static storage duration, then the language standard requires that the initializer be a constant expression. As the compiler rightly says, this is not a legal constant expression.
This expression could be evaluated at compile time, but that would be extending the language beyond the standard. In practice, it would require a more complex format for object files emitted by the compiler and a more intelligent linker capable of complex address arithmetics. That would be too much complexity just for one questionable feature.
0 Kudos
Reply

437 Views
mjbcswitzerland
Specialist V
Hi

I have notices similar things with CW.

It is (probably) due to array[] not being a real constant but rather being built on the stack. In this case it is not possible for the linker to deliver a const pointer value (since it is only known at run time).
I expect that you have array[] only actually alive in a limited part of code, like
case 1:
{
const u32 array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
u08* const ptr = (u08* const)  ( (u32)&array[16] & ~0xF );
}

This means that the contents of the array are 'handled' as const (no changes are allowed by code) but are not necessarily consts in FLASH.
If you add static it should solve it. This forces them to be committed to FLASH.

static const u32 array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};

Regards

Mark

www.uTasker.com




0 Kudos
Reply