[Fwd: Re: initializing ints with mem refs (ANSI C??)]

Ralf Guetlein ralf.guetlein@aranea.de
Sun Mar 5 09:29:00 GMT 2000


Moogla,
thank you for your reply

> 
> Ralf Gütlein wrote:
> 
> > const int object = 0;
> > int a[2] = {1, (const int)&object;};

Moogla wrote:
> Is there any problem with:
> const int object = 0;
> int a[2];
> a[0] = 1;
> a[1] = (const int) &object;

I should have written:

enum {item1, item2, item3, item4};

const int menu1[] = {item1, item2, item3};
const int menu2[] = {item4, (const int)menu1};

What I intended to do is generating a linked list in ROM
to implement a hierachical menu structure. In gcc this
is no problem, as I stated earlier.

>
> > What does the ANSI standard demand here?
> 
> ANSI only requires it to minimally be a constant expression, one that
> can be evaluated at compile time. Of course, we know the address of
> object at compile time, but strictly, it's not a constant. 

The difference between 'item1' and '(const int)menu1' is that the latter
is evaluated at *link* time. So you are right in that it is not a
*compile* time constant. This is no problem for gcc.

But IMO this should be generally no problem because

typedef void* PVOID;

enum {item1, item2, item3, item4};

const PVOID menu1[] = {(PVOID)item1, (PVOID)item2, (PVOID)item3};
const PVOID menu2[] = {(PVOID)item4, (PVOID)menu1};

works with both compilers, although '(PVOID)menu1' again can be
calculated only at link time.
(The assembler listing issued by gcc is exactly the same for both
of the above contructs, and the IAR assembler listing for the 
latter one is functionally identical to those made by gcc.)


> a subexpression elimination pass which rejects address references, and
> this pass is used to verify aggregate initializers. Also, IAR may feel
> it wants to move addresses of variables around in the data segment if
> it's feeling particularly optimizationy or whatever. GCC, oth, only
> resolves the initialization until it knows what the final value will be.
> I don't think you can count on that in future releases of EGCS, though.
> 

I guess the compiler behaviour in these cases depends in no way on
any kind of optimization. It's merely a restriction implemented by
the compiler writers (with or without intent).

> moogla

Thanks again. Regards,

Ralf


More information about the Gcc-help mailing list