473,508 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

valarray Initialization Question

Stroustrup page 663 says this:

valarray<floatv1 (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<floatv1 (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<floatv1 (0.0, 1000);

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

Thanks!

-Alex
Mar 14 '07 #1
3 2871
Alex Howlett wrote:
>
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/
Mar 22 '07 #2
Grizlyk <gr******@yandex.ruwrote:
Alex Howlett wrote:
>>
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;
}
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 22 '07 #3
Alex Howlett <a.l_e.x_AT.s_u.n_c.h_o.D@t_c.o_mwrote:
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....b44a8b786b6a79
(read Dietmar Kuehl's reply to my post as well)

Allegedly this was fixed in 2005:
http://groups.google.com/group/micro...aa1c4286?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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3644
by: Christian Brechbühler | last post by:
The template std::valarray behaves pretty much like a mathematical vector. Arithmetic operators apply elementwise. Now I'd like to extend this to a user-defined type, e.g., complex. ...
0
1707
by: Prune Tracy | last post by:
I have a question about assignment of slices of valarrays. I want to assign one general slice of a valarray to another. It seems the only way is to use the static_cast syntax, but this seems to...
1
1293
by: Milan | last post by:
Just a few lines: 1: valarray<int> t(0, 10); 2: for (int i = 0; i < 10; ++i) 3: t = i; 4: valarray<int> t2 = t; Line 4 causes a core dump with g++. Why? Besides, if I...
1
1769
by: ES Kim | last post by:
comp.std.c++ would be a better place for this question. Forgive me, but I can't post anything on moderated newsgroups for some reason. valarray doesn't have iterators of its own, which makes...
1
1365
by: ES Kim | last post by:
Here's a code fragment from TC++PL, p679: void f(valarray<double>& v) { size_t i = { 3, 2, 1, 0 }; valarray<size_t> index(i, 4); valarray<double> vv = log(v); } Every compiler I tried...
5
1514
by: Dave | last post by:
#include <iostream> #include <valarray> using namespace std; int main() { valarray<int> v(10); v = 42; v = 243;
2
2035
by: Michael Hopkins | last post by:
Hi all I have a subclass of valarray<T> thus template <typename T> class uo_val : public std::valarray<T> { public: uo_val ( ) : std::valarray<T>() {} uo_val (const int sz ) :...
0
1577
by: Clemens Hintze | last post by:
Hello, I have a question concerning the usage of default constructed std::slice instances. Our company currently validate the GNU-G++ 3.4 compiler against the ISO/IEC 14882:2003 standard for...
1
2555
by: Dack | last post by:
Hi, I want to track memory leaks in my application (that is using <valarray>). I used the following code: #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> But then, when I...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.