Extern keyword and "error: initializer element is not constant" after definition.

Joined
May 1, 2017
Messages
5
Reaction score
3
I have the following snippet of C code lifted fro a large mass of legacy code. having managed to compile a 120 parts of various libraries, but I am stuck on this one extern problem my brains just starting to dry up.
Any one have a solution please.

Tn it's original form the snippet read:

A)
............
extern end;

unsigned Lastbrk = (unsigned) &end;
..............

This produced : "error: initializer element is not constant"
fair comment as end has only been declared, not been defined no memory allocated.
----------------------------------------------------------------------

I modified it to read :
B)
......................
extern int end = 0;

unsigned Lastbrk = (unsigned) &end;

........................

My reasoning is that, the C standard says (from memory): that if a variable is not only declared extern but an initializer is also provided with that declaration, then the memory for that variable will be allocated–in other words, that variable will be considered as defined.

But I still get "error: initializer element is not constant" Which is not true as end has been initalized so &(end) will have a known value ?

Is this a gcc bug or what? I am using an old gcc 4.7.2 but about to switch Sun_Workshop on Solaris 8.

any help appreciated,
OBB (Dave)
 
Joined
May 1, 2017
Messages
5
Reaction score
3
SOLVED caused by bug in gcc!
I conclude the problem is a bug is gcc as when I compiled the c file from which the snippet was extracted using the Sun Workshop 5 C compiler it created the .o file without warning, error or exception.


I have the following snippet of C code lifted fro a large mass of legacy code. having managed to compile a 120 parts of various libraries, but I am stuck on this one extern problem my brains just starting to dry up.
Any one have a solution please.

Tn it's original form the snippet read:

A)
............
extern end;

unsigned Lastbrk = (unsigned) &end;
..............

This produced : "error: initializer element is not constant"
fair comment as end has only been declared, not been defined no memory allocated.
----------------------------------------------------------------------

I modified it to read :
B)
......................
extern int end = 0;

unsigned Lastbrk = (unsigned) &end;

........................

My reasoning is that, the C standard says (from memory): that if a variable is not only declared extern but an initializer is also provided with that declaration, then the memory for that variable will be allocated–in other words, that variable will be considered as defined.

But I still get "error: initializer element is not constant" Which is not true as end has been initalized so &(end) will have a known value ?

Is this a gcc bug or what? I am using an old gcc 4.7.2 but about to switch Sun_Workshop on Solaris 8.

any help appreciated,
OBB (Dave)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top