Static array initialization

M

Michael W. Hicks

Hello,

Consider the following code snippet:

int x[ 3 ] = { 1, 2 };

What is the value of x[ 2 ]?

My assumption was that this value is undefined and dependent upon the
compiler implementation, as well as the compilers settings. However, I
recently took a C++ programmer's test and the test stated that the value of
x[ 2 ] is 0. Doing a quick test with a sample debug program on Visual C++
6.0 affirms that the value of x[ 2 ] is 0. Is this a C++ standard directive
and am I wrong? Or was the test hogwash?

Thanks,
Mike
 
R

Ron Natalie

Michael W. Hicks said:
Hello,

Consider the following code snippet:

int x[ 3 ] = { 1, 2 };

What is the value of x[ 2 ]?

Zero. Any elements in the aggregate not given initializers are default initialized.
For int's that means zero.
Or was the test hogwash?

You're wrong, but there's no way to "test" for it really. If it really was an
indeterminate value, how would you test for it. It's possible that the indeterminate
value just happened to be zero during the test.
 
M

Michael W. Hicks

Ron Natalie said:
Hello,

Consider the following code snippet:

int x[ 3 ] = { 1, 2 };

What is the value of x[ 2 ]?

Zero. Any elements in the aggregate not given initializers are default initialized.
For int's that means zero.

Okay, thanks for the clarification.
You're wrong, but there's no way to "test" for it really. If it really was an
indeterminate value, how would you test for it. It's possible that the indeterminate
value just happened to be zero during the test.

Yes, you're correct that one could not test for the value with complete
certainty. However, if millions of tests showed the value to always be 0,
then there's a good chance that the value will always be 0. Of course, it's
still not certain... =)

- Mike
 
J

Jeremy Cheung

Michael W. Hicks said:
Ron Natalie said:
Hello,

Consider the following code snippet:

int x[ 3 ] = { 1, 2 };

What is the value of x[ 2 ]?

Zero. Any elements in the aggregate not given initializers are default initialized.
For int's that means zero.
Okay, thanks for the clarification.
Yes, you're correct that one could not test for the value with complete
certainty. However, if millions of tests showed the value to always be 0,
then there's a good chance that the value will always be 0. Of course, it's
still not certain... =)

However, I think it's not good to depend on "compiler initialization
value" in programming.
 
N

Nick Hounsome

Jeremy Cheung said:
Michael W. Hicks said:
Ron Natalie said:
Hello,

Consider the following code snippet:

int x[ 3 ] = { 1, 2 };

What is the value of x[ 2 ]?

Zero. Any elements in the aggregate not given initializers are
default
initialized.
For int's that means zero.
Okay, thanks for the clarification. really
was an the
indeterminate
Yes, you're correct that one could not test for the value with complete
certainty. However, if millions of tests showed the value to always be 0,
then there's a good chance that the value will always be 0. Of course, it's
still not certain... =)

However, I think it's not good to depend on "compiler initialization
value" in programming.

Why not? You depend on it initializing the first 2 elements.
What you shouldn't do is depend on empirical 'evidence'.
This will always work on every compiler because it goes back to very old C.
 
M

Martijn Lievaart

Consider the following code snippet:

int x[ 3 ] = { 1, 2 };

What is the value of x[ 2 ]?

Zero. Any elements in the aggregate not given initializers are default
initialized.
For int's that means zero.


However, I think it's not good to depend on "compiler initialization
value" in programming.

It's very debatable if this is "compiler initialization value". To most
this would be the programmer asking the rest of the array to be zero
initialised.
Why not? You depend on it initializing the first 2 elements.
What you shouldn't do is depend on empirical 'evidence'.
This will always work on every compiler because it goes back to very old C.

Besides, it is a common idiom, so wether you like it or not, we're pretty
much stuck with it.

HTH,
M4
 

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

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top