not computable at load time?

C

copx

What is wrong with the following code? My compiler (GCC) produces a warning
which states that the initialisation values of a local struct are "not
computable at load time". A warning is not equal to an error, but it usually
suggests that the code is at least questionable. I do not see anything
questionable...

This program demonstrates the issue. I get the warning whenever I pass a
pointer to a structure and use the fields of that structure to initalise the
values of a local structure. What is not kosher about that?

===

typedef struct {
int *ab;
int *ac;
} TEST;

typedef struct {
int *b;
int *c;
} TESTO;

void testo(TEST *aba)
{
/* warning - not computable at load time? */
TESTO kd = {aba->ab, aba->ac};
}


int main(void)
{
TEST aba;

testo(&aba);
return 0;
}


OK, the fields of aba are not initalised in this demo code, but that does
not matter. The warning shows up even if all fields of the passed structure
contain valid values.
 
J

Jack Klein

What is wrong with the following code? My compiler (GCC) produces a warning
which states that the initialisation values of a local struct are "not
computable at load time". A warning is not equal to an error, but it usually
suggests that the code is at least questionable. I do not see anything
questionable...

This program demonstrates the issue. I get the warning whenever I pass a
pointer to a structure and use the fields of that structure to initalise the
values of a local structure. What is not kosher about that?

===

typedef struct {
int *ab;
int *ac;
} TEST;

typedef struct {
int *b;
int *c;
} TESTO;

void testo(TEST *aba)
{
/* warning - not computable at load time? */
TESTO kd = {aba->ab, aba->ac};
}


int main(void)
{
TEST aba;

testo(&aba);
return 0;
}


OK, the fields of aba are not initalised in this demo code, but that does
not matter. The warning shows up even if all fields of the passed structure
contain valid values.

All versions of the C standard prior to 1999 required that
initializers for an automatic aggregate (structure or array) must be
constant expressions.

In C, the value of an object, even a const object, is NEVER a constant
expression. Some compilers (quite a few, actually) accept this as an
extension. All the gcc is doing is warning you that it is accepting
this code as an extension, that it is not standard C, and it might not
be portable to other compilers that don't provide the same extension.

From 1999 on, this rule was changed in the C standard, and the code
you have written is valid in any compiler that conforms to the 1999 or
later version of the C standard. Depending on how old your version of
gcc is, you might be able to invoke it in C99 conforming mode, in
which case the warning should disappear.

But then the code still might no be portable to other compilers that
conform to C99.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
C

copx

Jack Klein said:
All versions of the C standard prior to 1999 required that
initializers for an automatic aggregate (structure or array) must be
constant expressions.

In C, the value of an object, even a const object, is NEVER a constant
expression. Some compilers (quite a few, actually) accept this as an
extension. All the gcc is doing is warning you that it is accepting
this code as an extension, that it is not standard C, and it might not
be portable to other compilers that don't provide the same extension.

From 1999 on, this rule was changed in the C standard, and the code
you have written is valid in any compiler that conforms to the 1999 or
later version of the C standard. Depending on how old your version of
gcc is, you might be able to invoke it in C99 conforming mode, in
which case the warning should disappear.

I see. Thanks!

[snip]
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top