Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've read this article: https://isocpp.org/wiki/faq/containers#arrays-are-evil[^]

Should I almost always use C++ containers overs standard C arrays?
Another question, as a novice, I want ask whether containers such as vector always put their elements on the heap? thus there's no need for new and delete[]?
Posted
Updated 17-Dec-15 15:46pm
v2
Comments
PIEBALDconsult 17-Dec-15 20:48pm    
What's an STL? It definitely sounds like something best avoided.
Richard MacCutchan 18-Dec-15 7:13am    
Sexually Transmitted Library :)
Are Riff 17-Dec-15 21:45pm    
I mean standard C++ containers.
kendokumar 18-Dec-15 6:51am    
heap is a dynamic storage memory and stack is a static storage memory

There's no reason to use a C style array unless you're using a C++'98 or '03 compiler. Anywhere you'd consider using a built in array (i.e. you want something that's O(1) complexity random accessible) use code>vector<t> if you want it resizable and array<t,n> otherwise.

Most containers use the heap but they don't have to. Every implementation I've seen of array<t,n> doesn't use the heap and at least one implementation of vector<t></t> I've seen had a small vector optimisation for low count vectors of small objects that didn't go near the heap. Generally though if you assume a container (apart from array<t,n>) uses the heap you won't go far wrong.
 
Share this answer
 
horses for courses ... generally tho, stl containers are better (IMO) *as long as you choose the right one for the right job* i.e. understand the difference in behaviour between a deque and a vector

yes - they heapify - if you're going to use them for classes, understand the copy constructor
 
Share this answer
 
I'm a bit late but ...

The wiki link provided has some bad examples. Bad in that they wouldn't work for either STL vectors or arrays.

The wiki article also claims "Since a C array doesn't know its size, there can be no array assignment". That's very short-sighted.

An array is a pointer in C. If the array's declaration is in-scope, the sizeof operator can deduce the array count. Unfortunately this does not survive a function call.

C++
static void sadness(const int array[30])
{
    /* this doesn't do what you think. */
    printf("array size is %d", sizeof array);
}


One way around this is to wrap the array declaration inside a struct. As long as the struct definition is in-scope, the sizeof operator will work, as will copy assignment.

C++
struct buffer
{
    int array[30];
};

static void happiness(const buffer *buf)
{
    printf("array size is %d", sizeof buf->array);
}


That said, if you truly need variable sized arrays - an STL vector is the way to go. I often use STL vector class members for arrays just so the class destructor can take care of clean-up for me to avoid memory leaks.

At some point you may find yourself challenged to squeeze more performance when crunching lots of data in large arrays / vectors.

The STL array operator is slower that using a bare pointer so I often find myself grabbing a pointer to the data and using that inside a compute intensive loop.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900