valarray problem

D

Dave

#include <iostream>
#include <valarray>

using namespace std;

int main()
{
valarray<int> v(10);
v[4] = 42;
v[5] = 243;

int *p;

p = &v[4];
cout << *p << endl;

p = &v[5];
cout << *p << endl;

p = &v[6];
cout << *p << endl;
}

This outputs:
42
243
-842150451

Should not the last line of output be 0? If not, why not?

Thanks,
Dave
 
E

Einar Forselv

valarray<int> v(10);

Check the valarray documentation and use another constructor.
As far as i can see in your code, you dont give valarray[6] any value.
None of the values in your array have been initialized. Thats why you
get a random number.

Try this, and it would give the same problem :

int i;
cout << i;

while this one will print a 0

int i = 0;
cout << i;

I think valarray<int> v(0, 10); will set all the elements an initial
value of 0. But check the docs. ;)
 
D

Dave

Einar Forselv said:
valarray<int> v(10);

Check the valarray documentation and use another constructor.
As far as i can see in your code, you dont give valarray[6] any value.
None of the values in your array have been initialized. Thats why you
get a random number.

Try this, and it would give the same problem :

int i;
cout << i;

while this one will print a 0

int i = 0;
cout << i;

I think valarray<int> v(0, 10); will set all the elements an initial
value of 0. But check the docs. ;)

While your examples are correct, valarray behaves differently. It is
required to initialize all elements to 0 unless an initialization value is
specified.
 
E

Einar Forselv

Dave said:
While your examples are correct, valarray behaves differently. It is
required to initialize all elements to 0 unless an initialization value is
specified.

You are right. I tried the example you posted, and it worked for me.
Even a valarray of 100000 elements showed that all of them was 0. I have
no idea what it could be.
 
D

Dave

Einar Forselv said:
You are right. I tried the example you posted, and it worked for me.
Even a valarray of 100000 elements showed that all of them was 0. I have
no idea what it could be.

AFAICT, it's just a bug in my Standard Library implementation. But I wanted
to double-check since there are a lot of ancillary classes associated with a
valarray (that maybe I was using indirectly and could not tell) and I'm not
sure what would happen of unary & were applied to them...
 
M

msalters

Dave schreef:
#include <iostream>
#include <valarray>

using namespace std;

int main()
{
valarray<int> v(10);
v[4] = 42;
v[5] = 243;

int *p;

p = &v[4];
cout << *p << endl;

p = &v[5];
cout << *p << endl;

p = &v[6];
cout << *p << endl;
}

This outputs:
42
243
-842150451

Should not the last line of output be 0? If not, why not?

Looks like valarray<T>::eek:perator[] doesn't return a T&. I think it's
because valarray was designed as an array class, and not as an int[].
That niche would be occupied by vector<int> (which does hold an int[])

Regards,
Michiel Salters
 

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

Similar Threads


Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top