By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,111 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,111 IT Pros & Developers. It's quick & easy.

valarray problem

P: n/a
#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
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> 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. ;)

Jul 23 '05 #2

P: n/a

"Einar Forselv" <en****@SPAMcontraz.no> wrote in message
news:j2FBe.756$jA4.133@amstwist00...
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.
Jul 23 '05 #3

P: n/a
Dave wrote:
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.
Jul 23 '05 #4

P: n/a

"Einar Forselv" <en****@SPAMcontraz.no> wrote in message
news:pqFBe.757$jA4.525@amstwist00...
Dave wrote:
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.


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...
Jul 23 '05 #5

P: n/a
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>::operator[] 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

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.