------- Comment #5 from fang at csl dot cornell dot edu  2006-11-03 07:28 
-------
> There is no argument against the ISO standard, but to a non C/C++ programmer 
> it seems a waist of time to reallocate the array and initialize it when one 
> wants to add something to an array. Some other compilers will copy the 
> existing data into the newly allocated space which seems more in line with 
> the text book.
> The application will be changed to avoid the loss of data on a resize of 
> arrays, which is a better idea anyway. Thanks for the speedy reply.

If you really want efficient appending to a dynamically allocated array, then
use std::vector, which is capable of pre-allocating more memory than the number
of elements, using the reserve() member function.  From looking in the
implementation, N push_back operations result in lg N reallocations, by
doubling the reserve size each time the capacity is exceeded.  vector achieves
this by tracking three pointers: beginning, end-of-elements (size),
end-of-storage (capacity).  valarray is lighter-weight, having only two
pointers, beginning- and end-of-storage; thus it must reallocate upon resize
(otherwise, how could size() be reported correctly?).  I'd only use valarray
when the sizeof(container) is crucial and/or the size of the container is
unlikely/infrequently to change at run-time.  


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29688

Reply via email to