Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > dynamically allocated array and vector

Reply
Thread Tools

dynamically allocated array and vector

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      04-12-2010
We have std::vector<T> container for an element type 'T' that
satisfies the element requirements - like Copy Constructible,
Assignable. We also have dynamically allocated arrays. When will we
use std::vector and when will we use dynamically allocated arrays ?
Kindly explain, if possible with sample program.

Thanks
V.Subramanian
 
Reply With Quote
 
 
 
 
Anand Hariharan
Guest
Posts: n/a
 
      04-12-2010
On Apr 11, 9:33*pm, "(E-Mail Removed), India"
<(E-Mail Removed)> wrote:
> We have std::vector<T> container for an element type 'T' that
> satisfies the element requirements - like Copy Constructible,
> Assignable. We also have dynamically allocated arrays. When will we
> use std::vector and when will we use dynamically allocated arrays ?
> Kindly explain, if possible with sample program.
>


Most suggest to prefer vector over arrays whose sizes are specified at
compile time. There is no reason to use dynamically allocated arrays
over vector. Using new [] or malloc is way more tedious.

- Anand
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-12-2010
* http://www.velocityreviews.com/forums/(E-Mail Removed), India:
> We have std::vector<T> container for an element type 'T' that
> satisfies the element requirements - like Copy Constructible,
> Assignable. We also have dynamically allocated arrays. When will we
> use std::vector and when will we use dynamically allocated arrays ?
> Kindly explain, if possible with sample program.


Generally a std::vector is preferable to a directly dynamically allocated array,
since std::vector takes care of deallocation automatically and has much useful
funtionality, and is just as efficient.

However, in some cases you need to use library code that takes ownership of an
array and deallocates it in some special way (e.g. by using 'free').

In such cases you need to allocate the array directly, using the form of
allocation matching the expected deallocation (e.g., using 'malloc').

Another reason to allocate an array yourself might be for optimization purposes,
e.g. sub-allocating from some larger block that is deallocated in one operation.
std::vector supports also such custom allocation. But it might be simpler to
just do it yourself than to cajole std::vector into doing it.

There might also be other reasons to go the DIY route, but offhand I can't think
of any.


Cheers & hth.,

- Alf
 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      04-12-2010
On 12 Apr., 04:33, "(E-Mail Removed), India"
<(E-Mail Removed)> wrote:
> We have std::vector<T> container for an element type 'T' that
> satisfies the element requirements - like Copy Constructible,
> Assignable. We also have dynamically allocated arrays. When will we
> use std::vector and when will we use dynamically allocated arrays ?
> Kindly explain, if possible with sample program.
>


If you want a dynamic size, you will probably always want to use
std::vector. Dynamic allocation of arrays is there primarily because
std:vector was not available in C++ before templates were available.
If you need a fixed size array, you might need something like
boost::array. Otherwise, I would still recommend std::vector.
Alf Steinbach mentioned a few rare cases where you just have to use
array new. I believe those cases are quite rare, and I myself have
never needed to use array new.

/Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Uniform vector class, inheriting from Array: How to make sure thatmethods return a Vector and not an Array? Thomas Ruby 7 05-23-2005 04:21 PM
Problems initializing a dynamically allocated 2D array masood.iqbal@lycos.com C++ 6 02-07-2005 06:55 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Dynamically allocated array question Jeff Williams C++ 6 08-19-2003 01:13 PM



Advertisments