Initializer element not computable at load time

C

Clint Olsen

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
 
J

Joe Wright

Clint said:
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?
 
E

Eric Sosman

Clint said:
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'.
 
C

CBFalconer

Clint said:
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.
 
I

Irrwahn Grausewitz

Eric Sosman said:
Clint said:
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
 
J

Jack Klein

Eric Sosman said:
Clint said:
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
 
I

Irrwahn Grausewitz

Jack Klein said:
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.
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top