emreakyilmaz
emreakyilmaz

Reputation: 137

Vector of vector pointer memory allocation

First I want to say that, I have a vector which has thousand of vectors inside. Each of these inside vectors has thousand of numbers inside. I want to keep memory management safe and memory usage at minimum as much as possible.

I want to ask that if I have a code similiar to below

    int size = 10;
vector<vector<double>>* something = new vector<vector<double>>(size);

vector<double>* insideOfSomething;
for(int i = 0; i < size; i++){
    insideOfSomething = &(something->at(i));
    //...
    //do something with insideOfSomething 
    //...
}

I know that 'something' will be created in heap. What I don't understand is where the vectors are placed, 'insideOfSomething' points? If they are created in stack, then this means that I have a vector pointer, which points a vector in heap, that has vectors inside which are created in stack? (I'm very confused right now.)

If I have a code similiar to the one below;

    vector<vector<double>*>* something = new vector<vector<double>*>(size);
vector<double>* insideOfSomething;
for(int i = 0; i < size; i++){
    something->at(i) = new vector<double>();
    insideOfSomething = something->at(i);
    //...
    //do something with inside insideOfSomething 
    //...
}

right know all of my vectors are stored in heap, right?

Which one is more usefull according to the memory management?

Upvotes: 2

Views: 4046

Answers (5)

Joe
Joe

Reputation: 57169

You should avoid allocating vectors on the heap and just declare them on the stack since the vector will manage its objects on the heap for you. Anywhere you want to avoid creating a copy you can just use a reference or const reference (which ever is necessary).

vector<vector<double> > something(size);

for(int i = 0; i < size; i++)
{
    vector<double> &insideOfSomething = something.at(i);

    //use insideOfSomething
}

Upvotes: 7

stinky472
stinky472

Reputation: 6797

Let's take a random, simplistic implementation of vector, as I think this will help you.

template <class T, class Alloc>
class vector
{
private:
    T* buffer;
    std::size_t vector_size;
    std::size_t vector_capacity
    Alloc alloc;

public:
    ...
};

In this case, if we write:

vector<int> v;
v.push_back(123);

... the pointer, buffer, the integrals: vector_size and vector_capacity, and the allocator object, alloc, will all be created on the stack (along with allocating any additional memory necessary for structure padding and alignment).

However, vector itself will allocate memory on the heap to which this buffer pointer will store its base address. That will always be on the heap and will contain the actual contents of the vector as we think of them.

This is still more efficient than this:

vector<int>* v = new vector<int>;
v->push_back(123);
...
delete v;

... as this would involve a heap allocation/deallocation for the vector itself (including its data members) in addition to the memory vector itself allocates for its internal contents (the buffer). It also introduces an additional level of indirection.

Now if we have a vector of Somethings (vector of vector or anything else):

vector<Something> v;

Those Something instances are always going to be allocated within a contiguous heap buffer since they would reside in the dynamically allocated memory blocks that vector creates and destroys internally.

Upvotes: 4

leftaroundabout
leftaroundabout

Reputation: 120711

I want to keep memory management safe and memory usage at minimum as much as possible.

Then

vector<vector<double>>* something = new vector<vector<double>>(size);

is already not good. As said in the other answers, vector already has its data on the heap, no need to mess around with new to achieve this. In fact, the objects' location is like

       S t a c k                              H e a p

                                        (vector<double>) sthng[0]
(vector<vector<double>>) sthng          (vector<double>) sthng[1]
                                                ...
                                             - - - - - -
                                         (double) sthng[0][0]
                                         (double) sthng[0][1]
                                                ...
                                             - - - - - -
                                         (double) sthng[1][0]
                                         (double) sthng[1][1]
                                                ...

(of course, there is no particular ordering of the blocks on the heap)

Upvotes: 1

Sid
Sid

Reputation: 7631

Joe and hired777's answers explain that a vector will be allocated on the heap no matter what. I'll try to give some insight on the reason for this.

A vector is a resizeable container. Generally it doubles in size when it reaches capacity which means it needs to be able to allocate more memory than it had already allocated. Hence even when you declare vector inside a function and hence on the stack, internally it's holding a pointer to it's data on the heap and on going out of the function's scope, it's destructor will delete this data from the heap.

Upvotes: 0

hired777
hired777

Reputation: 495

In vector<> all data stored in heap And i think you should simply use

vector< vector<double> > something;

Upvotes: 3

Related Questions