473,383 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Initializer element not computable at load time

What about the following is not computable? It seems that the size of foo
is easily computable:

typedef struct {
char *name;
char *data;
} Foo;

int main(void)
{
char data[] = { 'a', 'b', 'c', 'd' };

Foo foo = { "bar", data };

return 0;
}

Thanks,

-Clint
Nov 13 '05 #1
6 16916
Clint Olsen wrote:

What about the following is not computable? It seems that the size of foo
is easily computable:

typedef struct {
char *name;
char *data;
} Foo;

int main(void)
{
char data[] = { 'a', 'b', 'c', 'd' };

Foo foo = { "bar", data };

return 0;
}

I works here. What seems to be the problem?
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #2
Clint Olsen wrote:

What about the following is not computable? It seems that the size of foo
is easily computable:

typedef struct {
char *name;
char *data;
} Foo;

int main(void)
{
char data[] = { 'a', 'b', 'c', 'd' };

Foo foo = { "bar", data };

return 0;
}


The size of `foo' is known, yes. What's not known is
the address of the automatic ("stack-resident," on many
implementations) variable `data'.

--
Er*********@sun.com
Nov 13 '05 #3
Clint Olsen wrote:

What about the following is not computable? It seems that the
size of foo is easily computable:

typedef struct {
char *name;
char *data;
} Foo;

int main(void)
{
char data[] = { 'a', 'b', 'c', 'd' };
Foo foo = { "bar", data };
return 0;
}


The size is, but the contents are not (at compilation time). data
is a local variable (automatic), and where it will be located is
not known until main runs. Thus the initializer is NOT a compile
time constant.

If you declare data outside of main, as static, then the address
will be computable.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #4
Eric Sosman <Er*********@sun.com> wrote:
Clint Olsen wrote:

What about the following is not computable? It seems that the size of foo
is easily computable:

typedef struct {
char *name;
char *data;
} Foo;

int main(void)
{
char data[] = { 'a', 'b', 'c', 'd' };

Foo foo = { "bar", data };

return 0;
}


The size of `foo' is known, yes. What's not known is
the address of the automatic ("stack-resident," on many
implementations) variable `data'.


In C89 this is certainly true, but in C99 it _seems_ to be allowed,
but unfortunately I'm unable to find a corresponding section in the
standard. Maybe someone can help out with a quote? Thanks.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #5
On Sat, 18 Oct 2003 01:44:49 +0200, Irrwahn Grausewitz
<ir*******@freenet.de> wrote in comp.lang.c:
Eric Sosman <Er*********@sun.com> wrote:
Clint Olsen wrote:

What about the following is not computable? It seems that the size of foo
is easily computable:

typedef struct {
char *name;
char *data;
} Foo;

int main(void)
{
char data[] = { 'a', 'b', 'c', 'd' };

Foo foo = { "bar", data };

return 0;
}


The size of `foo' is known, yes. What's not known is
the address of the automatic ("stack-resident," on many
implementations) variable `data'.


In C89 this is certainly true, but in C99 it _seems_ to be allowed,
but unfortunately I'm unable to find a corresponding section in the
standard. Maybe someone can help out with a quote? Thanks.

Regards


C90 said this in the constraints section of 6.5.7, "Initialization":

========
All the expressions in an initializer for an object that has static
storage duration or in an initializer list for an object that has
aggregate or union type shall be constant expressions
========

C99 says this in the constraints section of 6.7.8, "Initialization":

========
All the expressions in an initializer for an object that has static
storage duration shall be constant expressions or string literals.
========

So initializing an aggregate (array or structure) or union type with a
non-constant expression is no longer a constraint violation provided
the aggregate does not have static storage duration.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #6
Jack Klein <ja*******@spamcop.net> wrote:
On Sat, 18 Oct 2003 01:44:49 +0200, Irrwahn Grausewitz
<ir*******@freenet.de> wrote in comp.lang.c:
Eric Sosman <Er*********@sun.com> wrote: <snip>
> The size of `foo' is known, yes. What's not known is
>the address of the automatic ("stack-resident," on many
>implementations) variable `data'.


In C89 this is certainly true, but in C99 it _seems_ to be allowed,
but unfortunately I'm unable to find a corresponding section in the
standard. Maybe someone can help out with a quote? Thanks.


C90 said this in the constraints section of 6.5.7, "Initialization":

========
All the expressions in an initializer for an object that has static
storage duration or in an initializer list for an object that has
aggregate or union type shall be constant expressions
========

C99 says this in the constraints section of 6.7.8, "Initialization":

========
All the expressions in an initializer for an object that has static
storage duration shall be constant expressions or string literals.
========

So initializing an aggregate (array or structure) or union type with a
non-constant expression is no longer a constraint violation provided
the aggregate does not have static storage duration.


Hmpf, I read the whole section but didn't read _between_ the lines.

Thank you very much.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Todd Nathan | last post by:
Hi. have this code and compiler problem. GCC 2.95.3, BeOS, error "initializer element is not constant" #ifdef FILEIO { static struct { char *sfn; FILE *sfd; } stdfiles = {
7
by: B. Wood | last post by:
I have a small program (see below) that demonstrates an error I am getting when I build the program on a linux platform. I first initialize an unsigned char array (lines 16). I then initialize...
4
by: bingfeng | last post by:
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",...
3
by: Levi Campbell | last post by:
Hi, I'm trying to debug an app someone else wrote called eMixer. Here's the log contents: cc -O3 -funroll-loops -c -o main.o main.c cc -O3 -funroll-loops -c -o nctgui.o nctgui.c cc -O3...
3
by: vib | last post by:
Hi there, I wish to get some advice on the problem I face in initializing a table. Here is the code. I've an array of strings, ptrNameString . I want to initialize a table, mySoftKeyTable...
1
by: emin.martinian | last post by:
When trying to compile python extensions written in C using "python setup.py build" on cygwin I get the following error: foo.c: initializer element is not constant foo.c: error: (near...
5
by: fred | last post by:
Hi, Can someone explain me why gcc-4.0 gives me the 'Initializer element is not constant' error with this code ? Everything seems to be constant here... #include <stdio.h> typedef struct {...
7
by: Paul Edwards | last post by:
On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the error "initializer element is not constant" on the below code. The code compiles fine with other compilers I have. Is this valid C89 code?...
2
by: copx | last post by:
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...
2
by: hankypan1 | last post by:
Hi All, I need a tree data structure for my application. It is the non - cyclic simple tree where i can have any number of children node and each child can recursively become a sub tree like a...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.