dynamically allocated array and vector

S

subramanian100in

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
 
A

Anand Hariharan

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
 
A

Alf P. Steinbach

* (e-mail address 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
 
P

peter koch

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top