valarray Initialization Question

A

Alex Howlett

Stroustrup page 663 says this:

valarray<float> v1 (1000); // 1000 elements with value
float()==0.0F

But when I run this program:

--------------------------------------
#include <valarray>
#include <iostream>

int main (int argc, char * argv[])
{
std::valarray<float> v1 (1000);
std::cout << v1[0];

return 0;
}
--------------------------------------

My output is this:
-4.31602e+008

My compiler is Microsoft Visual C++ 2005.

Unless I'm missing something, my compiler seems to be contradicting
Stroustrup. So I decided to have a look at the standard.

25.6.2.1 of the standard says this:

--------------------------------------
explicit valarray (size_t);

The array created by this constructor has a length equal to the value
of the argument. The elements of the array are constructed using the
default constructor for instantiating type T.
--------------------------------------

I searched around the standard a bit and I can't really find anything
about whether the POD types actually have default constructors or not.

Section 8.5 says this:
--------------------------------------
5. ...

To default-initialize an object of type T means:

- if T is a non-POD class type (clause 9), the default constructor for
T is called (and the initialization is illformed if T has no accessible
default constructor);
- if T is an array type, each element is default-initialized;
- otherwise, the object is zero-initialized
--------------------------------------

Now, I take that to mean that default-initializing an object of a POD
type is the same as zero-initializing it. The problem is that the
valarray constructor in question isn't said to default-initialize its
elements. It's said to construct them using the default constructor.

Section 12.6 says this:

--------------------------------------
1. When no initializer is specified for an object of (possibly
cv-qualified) class type (or array thereof, or the initializer has the
form (), the object is initialized as specified in 8.5.
--------------------------------------

But what about an object of POD type?

Obviously, I can do this to get the desired effect:

std::valarray<float> v1 (0.0, 1000);

but I'd still like to know exactly what the single-argument constructor
is actually supposed to do.

Thanks!

-Alex
 
G

Grizlyk

Alex said:
Section 8.5 says this:
--------------------------------------
5. ...

To default-initialize an object of type T means:

- if T is a non-POD class type (clause 9), the default constructor for T
is called (and the initialization is illformed if T has no accessible
default constructor);
- if T is an array type, each element is default-initialized;
- otherwise, the object is zero-initialized

At least this (zero-initialized) is strange, because as i know

{
int a;
a+=1;
return a;
}

will return undefined value, it is not the same as

{
int a(0);
a+=1;
return a;
}


--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
 
M

Marcus Kwok

Grizlyk said:
At least this (zero-initialized) is strange, because as i know

{
int a;
a+=1;
return a;
}

will return undefined value, it is not the same as

{
int a(0);
a+=1;
return a;
}

Try this:

{
int a = int();
a += 1;
return a;
}

I think the different types of initialization are: default-initialize,
value-initialize, and zero-initialize. For primitive types,
value-initialize and zero-initialize are the same, but
default-initialize leaves the value in an indeterminate state.
 
M

Marcus Kwok

Alex Howlett said:
Section 8.5 says this:
--------------------------------------
5. ...

To default-initialize an object of type T means:

- if T is a non-POD class type (clause 9), the default constructor for
T is called (and the initialization is illformed if T has no accessible
default constructor);
- if T is an array type, each element is default-initialized;
- otherwise, the object is zero-initialized
--------------------------------------

Also, sorry for not answering your original question, but VC++ .NET 2003
does not correctly value-initialize arrays; see the following two
threads:

http://groups.google.com/group/comp.lang.c++/browse_frm/thread/602375401ad4b39a/dab44a8b786b6a79
(read Dietmar Kuehl's reply to my post as well)

Allegedly this was fixed in 2005:
http://groups.google.com/group/micr...c/browse_thread/thread/b5d0dbefaa1c4286?tvc=2

I do not have 2005 installed so I cannot test it, but I wonder whether
the valarray issue you are experiencing is related to this bug.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top