Object Initialization

S

sarathy

Hi,
I read the following points in K&R "Section A8.7 Initialization".
Seems like i have a problem with them.

* All expressions in the initialization of constant object/array must
be constant expression. [OK,fine]

* Expressions in the initializer for an auto or register object or
array must likewise be constant expressions if the initializer is a
brace enclosed list. [ ????]

But

auto int a=10;
auto int b[]={a,a++};
works fine.

* If the initializer for an automatic object[auto or register] is a
single expression, it need not be a constant expression.

What does single expression mean here ?

Regards,
Sarathy
 
R

Richard Heathfield

sarathy said:

auto int b[]={a,a++};
works fine.

Someone told me that, in basketball, you have to bounce the
ball off the floor; you can't hold the ball and run around the
court. But I tried it and it works just fine. Obviously he
does not understand basketball!
-- Attr. Steve Summit
 
J

jaysome

Hi,
I read the following points in K&R "Section A8.7 Initialization".
Seems like i have a problem with them.

* All expressions in the initialization of constant object/array must
be constant expression. [OK,fine]

* Expressions in the initializer for an auto or register object or
array must likewise be constant expressions if the initializer is a
brace enclosed list. [ ????]

But

auto int a=10;
auto int b[]={a,a++};
works fine.

* If the initializer for an automatic object[auto or register] is a
single expression, it need not be a constant expression.

What does single expression mean here ?

That's a really good question. As far as I can tell, the C Standard
never really explains that. Not even the Appendix that lists all the
types of expressions mentions the term "single expression". Maybe it's
the same as a "primary expression"? Whatever it is, it certainly isn't
obvious.
 
K

Keith Thompson

sarathy said:
I read the following points in K&R "Section A8.7 Initialization".
Seems like i have a problem with them.

* All expressions in the initialization of constant object/array must
be constant expression. [OK,fine]

* Expressions in the initializer for an auto or register object or
array must likewise be constant expressions if the initializer is a
brace enclosed list. [ ????]

Right. (I *think* that restriction was lifted in C99; at least, I
can't find it in C99 6.7.8, and gcc complains about it with
"-ansi -pedantic" but not with "-std=c99 -pedantic". My copy of the
C90 standard isn't handy at the moment.)
But

auto int a=10;
auto int b[]={a,a++};
works fine.

That doesn't mean it's valid. Possibly your compiler supports it as
an extension; it would probably complain in conforming mode. (If
you're using gcc, see above.)
* If the initializer for an automatic object[auto or register] is a
single expression, it need not be a constant expression.

What does single expression mean here ?

A single expression is simply an expression. For example, in the
example above, 10 is a (single) expression, but {a,a++} is not an
expression; it's two expressions separated by a comma and enclosed in
braces.
 
K

Keith Thompson

jaysome said:
On 22 Jul 2006 01:14:38 -0700, "sarathy" <[email protected]>
wrote: [...]
* If the initializer for an automatic object[auto or register] is a
single expression, it need not be a constant expression.

What does single expression mean here ?

That's a really good question. As far as I can tell, the C Standard
never really explains that. Not even the Appendix that lists all the
types of expressions mentions the term "single expression". Maybe it's
the same as a "primary expression"? Whatever it is, it certainly isn't
obvious.

It becomes obvious as soon as you stop making it complicated. :cool:}

A "single expression" isn't some special kind of expression; it's just
an expression, with "single" being used in its ordinary English sense
(i.e., there's just one).

An initializer can either be a (single) expression or a list of
expressions separated by commas; the latter is not itself an
expression.
 
C

Chris Torek

sarathy said:
* Expressions in the initializer for an auto or register object or
array must likewise be constant expressions if the initializer is a
brace enclosed list. [ ????]

Right. (I *think* that restriction was lifted in C99; at least, I
can't find it in C99 6.7.8, and gcc complains about it with
"-ansi -pedantic" but not with "-std=c99 -pedantic". My copy of the
C90 standard isn't handy at the moment.)

It was indeed lifted in C99.

The restriction in C89 makes it possible for compilers to "pretend"
that, in:

void f(void) {
int arr[5] = { 1, 2, 3 };
...
}

you wrote:

void f(void) {
static int arr_init[5] = { 1, 2, 3 };
int arr[5];
memcpy(arr, arr_init, sizeof arr);
...
}

Without the restriction, the compiler actually has to work hard
sometimes (gosh, imagine that :) ). But it always seemed a little
silly to me, since C89 does allow things like:

void f(void) {
int v1 = call_some_func();
int v2[5] = { 1, 2, 3 };
int v3 = call_another_func();
char v4[] = "text";
...
}

which, if you are doing any kind of optimization at all in your
compiler, has to "save up" the initialization of v1 and v3 until
you have decided how much space to allocate for the activation
record for f() (since, in general, one should not call functions
until the activation record has been established). So being able
to stash the initializers for v2 and v4 in some alternative text
space, then memcpy() them into place, merely shortens the four
deferred initializations slightly -- the compiler could, as it
must for C99, take:

void f(void) {
int v1 = call_some_func();
int v2[5] = { v1, 0, v1, 0, v1 };
int v3 = call_another_func();
...
}

and generate code "as if" you wrote:

void f(void) {
int v2[5], v3, v1;
v1 = call_some_func();
v2[0] = v2[2] = v2[4] = v1;
v2[1] = v2[3] = 0;
v3 = call_another_func();
...
}

It really is not that hard. :)
 
S

sarathy

Hi,
The initialization

int c=2,d=4;
int a[]={c,d};

gave "Illegal Initialization" error when compiled with Turbo C
But the error was supressed in gcc.

Thanks all.
Regards,
Sarathy


Keith said:
sarathy said:
I read the following points in K&R "Section A8.7 Initialization".
Seems like i have a problem with them.

* All expressions in the initialization of constant object/array must
be constant expression. [OK,fine]

* Expressions in the initializer for an auto or register object or
array must likewise be constant expressions if the initializer is a
brace enclosed list. [ ????]

Right. (I *think* that restriction was lifted in C99; at least, I
can't find it in C99 6.7.8, and gcc complains about it with
"-ansi -pedantic" but not with "-std=c99 -pedantic". My copy of the
C90 standard isn't handy at the moment.)
But

auto int a=10;
auto int b[]={a,a++};
works fine.

That doesn't mean it's valid. Possibly your compiler supports it as
an extension; it would probably complain in conforming mode. (If
you're using gcc, see above.)
* If the initializer for an automatic object[auto or register] is a
single expression, it need not be a constant expression.

What does single expression mean here ?

A single expression is simply an expression. For example, in the
example above, 10 is a (single) expression, but {a,a++} is not an
expression; it's two expressions separated by a comma and enclosed in
braces.
 
C

Coos Haak

Op 22 Jul 2006 12:58:33 -0700 schreef sarathy:
Hi,
The initialization

int c=2,d=4;
int a[]={c,d};

gave "Illegal Initialization" error when compiled with Turbo C
But the error was supressed in gcc.
Please turn op the warnings.
If the declarations are on file scope (i.e. not inside a function)
gcc -ansi -pedantic -W -Wall -O
gives a series of errors. Never trust the compiler suppressing errors.
 
S

Steve Summit

Richard said:
sarathy said:
auto int b[]={a,a++};
works fine.

Someone told me that, in basketball, you have to bounce the
ball off the floor; you can't hold the ball and run around the
court. But I tried it and it works just fine. Obviously he
does not understand basketball!
-- Attr. Steve Summit

The original quote by Roger Miller (then at Verifone, not sure
where he is now) was

Somebody once told me that in basketball you can't hold
the ball and run. I got a basketball and tried it and it
worked just fine. He obviously didn't understand basketball.

[The original message is missing from google's cache, but see
<[email protected]>, or http://groups.google.com/
group/comp.lang.c/msg/9c6529ae1b85f008 .]

My own attempts at pithy quotes here always involve traffic
lights, e.g. "Dabbling in undefined behavior is a little like
running a red light at 3am: you probably won't get caught,
but it's still wrong."
 

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,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top