what's meaning of "warning: initializer element is not computable at load time"

B

bingfeng

I have some codes generated by perl, in which initialize some huge
struct,such as

PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "",
"configuration on a designated vlan", PRO_REQUIRED };
const char* TOS_network_spantree_set_0_para_1_emvalue[] = { "disable",
"enable", NULL };
PARA TOS_network_spantree_set_0_para_1 = { "", emENUM,
TOS_network_spantree_set_0_para_1_emvalue, "", "enable or disable STP",
PRO_REQUIRED };
PPARA TOS_network_spantree_set_0_para[] = {
&TOS_network_spantree_set_0_para_0,
&TOS_network_spantree_set_0_para_1,
NULL
}
......

gcc(C89 setting) report warnings about that, but C99 not. I ignore those
warnings and try to dump the struct, the program crashed after some output.
what wrong and how I can fix it?

thanks a lot
 
J

Jack Klein

I have some codes generated by perl, in which initialize some huge
struct,such as

PERL is off-topic here, and your code snippet is littered with types
not defined by C, so it is hard to tell.
PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "",

What is the definition of the type 'PARA'? It appears to be 'pointer
to pointer to char' or 'pointer to pointer to const char'.
"configuration on a designated vlan", PRO_REQUIRED };
const char* TOS_network_spantree_set_0_para_1_emvalue[] = { "disable",
"enable", NULL };
PARA TOS_network_spantree_set_0_para_1 = { "", emENUM,
TOS_network_spantree_set_0_para_1_emvalue, "", "enable or disable STP",

If I am right about the definition of 'PARA', the initialization of is
wrong. "" and "enable or disable STP" are string literals, but
'TOS_network_spantree_set_0_para_1_emvalue' decays into a pointer to
pointer to char, a different type.
PRO_REQUIRED };
PPARA TOS_network_spantree_set_0_para[] = {
&TOS_network_spantree_set_0_para_0,
&TOS_network_spantree_set_0_para_1,
NULL
}
......

gcc(C89 setting) report warnings about that, but C99 not. I ignore those
warnings and try to dump the struct, the program crashed after some output.
what wrong and how I can fix it?

thanks a lot

I would say you have two options, one is to post the definitions of
all the UPPER CASE types and values used in the code snippet. The
other is to talk to the person who wrote the PERL code that generates
this, as it seems to be incorrect.

By the way, it is a very good idea indicate the exact line of your
code snippet that the compiler indicated was the cause of the error.
 
R

Richard Bos

Jack Klein said:
I would say you have two options, one is to post the definitions of
all the UPPER CASE types and values used in the code snippet.
By the way, it is a very good idea indicate the exact line of your
code snippet that the compiler indicated was the cause of the error.

_And_ the exact text of the warning itself. It could warn about
"incompatible types" or about "uninitialised objects" or about "your
indentation style is ugly", and the solution would be different in each
case.

Richard
 
C

Chris Torek

gcc(C89 setting) report warnings about that, but C99 not. I ignore
those warnings and try to dump the struct, the program crashed after
some output. what wrong and how I can fix it?
[/QUOTE][/QUOTE]

Indeed -- although I suspect the problem causing the crash is something
else entirely.

_And_ the exact text of the warning itself. It could warn about
"incompatible types" or about "uninitialised objects" or about "your
indentation style is ugly", and the solution would be different in each
case.

As it happens, the exact text of the warning is in the "subject"
header line. But this is not always visible to everyone when
reading the message body, so it should be repeated in (or moved
to) the message body:

"warning: initializer element is not computable at load time"

This particular warning is one that GCC emits when you use GCC's
C89-extension that allows initializing aggregates with non-constant
values. For instance:

% cat t.c
void f(int x, int y) {
int a[2] = { x, y };
...
}

This code is allowed in C99, but not in C89. GCC allows it even
without -std=c99, but will issue a diagnostic when requested:

% cc -O2 -c -W -Wall t.c
% cc -ansi -pedantic -O2 -c t.c
t.c: In function `f':
t.c:3: warning: initializer element is not computable at load time
t.c:3: warning: initializer element is not computable at load time
% cc -std=c99 -pedantic -O2 -c t.c
%

(note that "-ansi" means the same as "-std=c89", and "-pedantic"
is the option that tells GCC to emit the required diagnostic).
 
B

bingfeng

thanks to all warmhearted persons at first!

I know the exact answer of that question: I initialized a huge struct in a
function then return the address of struct to caller! Auto variables are not
stored in static memory, so loader cannot load it properly. Of course, it's
a fundamental to use static variables in such case, but the parts are in
global at first. I didn't want to introduce too many global names to global
namespace, and it seems initialize such struct is impossible, so I move it
into a function scope and emit add static keyword(you know, all the code was
generated automatic by some other tool). The difference of result C89, C99
and C++
generated misled I compare the different features they supported, sigh!

As to the coredump, someone told me maybe too many local variables occupy a
lot of stack, and my test routine use recursive calls, all of that in eacess
of size of stack, so app crashed. I think him is right.

If gcc mention "local variable" or "auto variable", I would notice such
basic and important bug, but I spend four hours before I ask others view my
code!


Jack Klein said:
I have some codes generated by perl, in which initialize some huge
struct,such as

PERL is off-topic here, and your code snippet is littered with types
not defined by C, so it is hard to tell.
PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "",

What is the definition of the type 'PARA'? It appears to be 'pointer
to pointer to char' or 'pointer to pointer to const char'.
"configuration on a designated vlan", PRO_REQUIRED };
const char* TOS_network_spantree_set_0_para_1_emvalue[] = { "disable",
"enable", NULL };
PARA TOS_network_spantree_set_0_para_1 = { "", emENUM,
TOS_network_spantree_set_0_para_1_emvalue, "", "enable or disable STP",

If I am right about the definition of 'PARA', the initialization of is
wrong. "" and "enable or disable STP" are string literals, but
'TOS_network_spantree_set_0_para_1_emvalue' decays into a pointer to
pointer to char, a different type.
PRO_REQUIRED };
PPARA TOS_network_spantree_set_0_para[] = {
&TOS_network_spantree_set_0_para_0,
&TOS_network_spantree_set_0_para_1,
NULL
}
......

gcc(C89 setting) report warnings about that, but C99 not. I ignore those
warnings and try to dump the struct, the program crashed after some output.
what wrong and how I can fix it?

thanks a lot

I would say you have two options, one is to post the definitions of
all the UPPER CASE types and values used in the code snippet. The
other is to talk to the person who wrote the PERL code that generates
this, as it seems to be incorrect.

By the way, it is a very good idea indicate the exact line of your
code snippet that the compiler indicated was the cause of the error.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top