using nonconstant expressions in an initializer list

A

aarklon

Hi all,

Recently i was reading a c book which contained a section called C
pitfalls.

it had a paragraph on the following lines:-

-------------------------------------------------------------------
using nonconstant expressions in an initializer list
-------------------------------------------------------------------
struct s {int i;};
int foo(void);

struct s bar(void)
{
struct S s = {foo()}; /*problem here*/
return s;
}

Explanation:-

unlike other automatic variable initializers,the expressions in an
initializer list for a struct,union,or array must be constant
expressions. constant expressions are not values of constant qualified
objects.they are expressions that trivially(without the use of global
or flow analysis) can be evaluated by the compiler at compile time

constant expressions in initializers can include the following

1) arithmetic constant expressions comprised of arithmetic constants of
various types and sizeof expressions ex:- (sizeof(struct S) +7)/8 * 8

2) Null pointer constants

3) addresses of static duration objects,such as file scope variables or
of function designators


Now my questions are as follows:-

1) To what extent this explanation is true ??
2) I compiled the above code cleanly on a mingw 2.95.2-1 compiler !!
3) what is this global or flow analysis ??
 
F

Frederick Gotham

Aarklon posted:
2) I compiled the above code cleanly on a mingw 2.95.2-1 compiler !!


Make sure you're not using a C++ compiler (C++ allows what you're trying to
do).
 
K

Keith Thompson

Hi all,

Recently i was reading a c book which contained a section called C
pitfalls.

it had a paragraph on the following lines:-

-------------------------------------------------------------------
using nonconstant expressions in an initializer list
-------------------------------------------------------------------
struct s {int i;};
int foo(void);

struct s bar(void)
{
struct S s = {foo()}; /*problem here*/
return s;
}

Explanation:-

unlike other automatic variable initializers,the expressions in an
initializer list for a struct,union,or array must be constant
expressions. constant expressions are not values of constant qualified
objects.they are expressions that trivially(without the use of global
or flow analysis) can be evaluated by the compiler at compile time

I hope it said "const qualified", not "constant qualified".
constant expressions in initializers can include the following

1) arithmetic constant expressions comprised of arithmetic constants of
various types and sizeof expressions ex:- (sizeof(struct S) +7)/8 * 8

2) Null pointer constants

3) addresses of static duration objects,such as file scope variables or
of function designators


Now my questions are as follows:-

1) To what extent this explanation is true ??

It's true for C90. I think this restriction was relaxed in C99.
2) I compiled the above code cleanly on a mingw 2.95.2-1 compiler !!

Try compiling in strict C89C90 mode ("-ansi -pedantic").
3) what is this global or flow analysis ??

Some expressions can potentially be evaluated at compilation time by
analyzing data flow, i.e., where particular values came from. For
example:

int x = 10;
int y = 20;
int z = x + y;
printf("z = %d\n", z);

An optimizing compiler can determine that z's initial value is 30, and
might even replace the printf() call with
puts("z = 30");

To do so, it has to prove that the values of x and y cannot have
changed before the expression "x + y" is evaluated, and that the value
of z cannot have changed before the printf call.

C defines certain expressions as being constant, and requires them to
be evaluated at compilation time. The language's definition of a
constant expression is designed so that it doesn't depend on this kind
of analysis.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top