![]() |
Initialization of vectors in c++
Experimenting at home with visual c++, I see that int main()
{std::vector<double> vect(5);} creates a vector whose 5 initial values are all 0. Is this standard or might the five initial values be different from 0? I'm a bit surprised by this as I would expect vect to consist of five uninitialized doubles. Why is it that double x; introduces a double which is uninitialized and yet the above vect is initialized? Or is this just a matter of the definition of the c++ language which should just be accepted, and can't be derived from some other principle? Thank you, Paul Epstein |
Re: Initialization of vectors in c++
On Apr 13, 11:10 pm, pauldepst...@att.net wrote:
> Experimenting at home with visual c++, I see that int main() > {std::vector<double> vect(5);} creates a vector whose 5 initial > values are all 0. Is this standard or might the five initial values > be different from 0? I'm a bit surprised by this as I would expect > vect to consist of five uninitialized doubles. Why is it that double > x; introduces a double which is uninitialized and yet the above vect > is initialized? Or is this just a matter of the definition of the c++ > language which should just be accepted, and can't be derived from some > other principle? > Here you actually called explicit vector(size_type n, const T& value = T(), const Allocator& = Allocator()); so the question becomes what T() equals to. 8.5 7 An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized. 5 To zero-initialize an object of type T means: -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T; .... 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 ill-formed if T has no accessible default constructor); -- if T is an array type, each element is default-initialized; -- otherwise, the object is zero-initialized. so with T==int, int() == 0 |
Re: Initialization of vectors in c++
In article <03277800-7557-456d-96b8-
d4d164b4eb0b@s39g2000prd.googlegroups.com>, pauldepstein@att.net says... > Experimenting at home with visual c++, I see that int main() > {std::vector<double> vect(5);} creates a vector whose 5 initial > values are all 0. Is this standard or might the five initial values > be different from 0? I'm a bit surprised by this as I would expect > vect to consist of five uninitialized doubles. Why is it that double > x; introduces a double which is uninitialized and yet the above vect > is initialized? Or is this just a matter of the definition of the c++ > language which should just be accepted, and can't be derived from some > other principle? As others have pointed out, the value is guaranteed to be zero. What they haven't pointed out (directly) is that you can specify another value if you prefer. E.g.: std::vector<double> vect(5, 15.0); -- Later, Jerry. The universe is a figment of its own imagination. |
Re: Initialization of vectors in c++
Jerry Coffin wrote:
> As others have pointed out, the value is guaranteed to be zero. Sometimes I feel this is counter-productive speedwise. If I want to allocate a large array of, for example, integers and I don't need for them to be initialized to anything (eg. because right after the allocation I initialize them manually to some values), the std::vector will uselessly go through the allocated array, initializing every value with 0, right after which I go again through the vector and initialize the values to something else. This is wasted time. If I allocate the array with 'new', it won't do anything to it and it will be much faster. |
Re: Initialization of vectors in c++
"Juha Nieminen" <nospam@thanks.invalid> wrote in message > Jerry Coffin wrote: >> As others have pointed out, the value is guaranteed to be zero. > > Sometimes I feel this is counter-productive speedwise. > > If I want to allocate a large array of, for example, integers and I > don't need for them to be initialized to anything (eg. because right > after the allocation I initialize them manually to some values), the > std::vector will uselessly go through the allocated array, initializing > every value with 0, right after which I go again through the vector and > initialize the values to something else. This is wasted time. > > If I allocate the array with 'new', it won't do anything to it and it > will be much faster. I think any statement without doing any actual measurements may not be true here. Also it would be a QoI issue. -- http://techytalk.googlepages.com |
Re: Initialization of vectors in c++
Andy Champ wrote:
> To follow from Vladislav's post: to reserve() the space, then > initialise each one as you go, is most efficient. Yes, reserve() plus initializing with push_back() may be an improvement to the situation. However, I wonder if push_back() is as fast as a direct "array[index] = value;". |
Re: Initialization of vectors in c++
Victor Bazarov schrieb:
> Juha Nieminen wrote: >> Andy Champ wrote: >>> To follow from Vladislav's post: to reserve() the space, then >>> initialise each one as you go, is most efficient. >> Yes, reserve() plus initializing with push_back() may be an >> improvement to the situation. However, I wonder if push_back() is as >> fast as a direct "array[index] = value;". > > The difference is that 'push_back' copy-constructs using placement > 'new', whereas the other way would default-construct and then assign. > Basically, the total difference would be the final 'size()' times the > difference between {def-construct + assign} vs {copy-construct}. push_back() has to test the need for reallocation on each call, that makes it hard to unroll the loop and is a mess for the instruction pipeline etc... > > But it would have to be measured, not calculated. Thats the only way. -- Thomas http://www.netmeister.org/news/learn2quote.html "Some folks are wise, and some otherwise." |
Re: Initialization of vectors in c++
Thomas J. Gritzan wrote:
> push_back() has to test the need for reallocation on each call, that > makes it hard to unroll the loop and is a mess for the instruction > pipeline etc... And if we are using something like OpenMP, push_backs cannot be performed in parallel (while direct array initializations can, given that the values do not depend on each other). |
Re: Initialization of vectors in c++
On Apr 13, 8:57 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Jerry Coffin wrote: > > As others have pointed out, the value is guaranteed to be zero. > Sometimes I feel this is counter-productive speedwise. You mean you've actually had cases where an application wasn't fast enough because of it? > If I want to allocate a large array of, for example, integers and I > don't need for them to be initialized to anything (eg. because right > after the allocation I initialize them manually to some values), the > std::vector will uselessly go through the allocated array, initializing > every value with 0, right after which I go again through the vector and > initialize the values to something else. This is wasted time. You mean you've actually had an application which wasn't fast enough because of this? Of course, it's really very rare to initialize a vector like this unless you actually want all of the values to have the same value. Usually, you'll provide two iterators which generate the correct values. And yes, I know, it would be a lot easier and a lot more natural if only one were necessary, but Boost iterator can take a lot of the pain out of it. > If I allocate the array with 'new', it won't do anything to it > and it will be much faster. Using uninitialized memory to generate random results is probably faster than anything you can do with std::vector, yes. -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
Re: Initialization of vectors in c++
On Apr 15, 12:01 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> Juha Nieminen wrote: > > Andy Champ wrote: > >> To follow from Vladislav's post: to reserve() the space, > >> then initialise each one as you go, is most efficient. > > Yes, reserve() plus initializing with push_back() may be an > > improvement to the situation. However, I wonder if > > push_back() is as fast as a direct "array[index] = value;". > The difference is that 'push_back' copy-constructs using > placement 'new', whereas the other way would default-construct > and then assign. Basically, the total difference would be the > final 'size()' times the difference between {def-construct + > assign} vs {copy-construct}. > But it would have to be measured, not calculated. The one time I measured, creating a vector< double > with all of the elements, then assigning, was faster than using puch_back. On my particular implementation. (I forget now whether I did the measurement on a Sparc under Solaris or a PC under Linux.) If speed is critical, use the two iterators constructor, and make sure it's a random access iterator. It's a little more work, but the case probably comes up so rarely that that's not an issue. -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
| All times are GMT. The time now is 08:30 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.