Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a vector allocated on the "stack" be passed from function to function?

I know that variables allocated on that stack of a function become inaccessible when the function finishes execution. However, vector types allocate their elements on the heap no matter how they are allocated. So for instance,

vector<int> A;

will allocate space for its elements on the heap instead of the stack.

My question is, assume I have the following code:

int main(int argc, char *argv[]) {
    // initialize a vector
    vector<int> A = initVector(100000);

    // do something with the vector...

    return 0;
}


// initialize the vector
vector<int> initVector(int size) {
    vector<int> A (size);  // initialize the vector "on the stack"

    // fill the vector with a sequence of numbers...
    int counter = 0;
    for (vector<int>::iterator i = A.begin(); i != A.end(); i++) {
        (*i) = counter++;
    }

    return A;
}

Will I have memory access problems when using the vector A in the main function? I tried this several times and they all worked normally, but I'm scared that this might just be luck.

The way I see it is, the vector A allocates its elements on the heap, but it has some "overhead" parameters (maybe the size of the vector) allocated on the stack itself. Therefore, using the vector in the main function might result in a memory access problem if these parameters are overwritten by another allocation. Any ideas?

like image 539
alguru Avatar asked Apr 06 '12 18:04

alguru


2 Answers

When you do "return A;" you return by value, so you get a copy of the vector -- C++ creates a new instance and calls copy constructor or operator= on it. So in this case it doesn't matter where the memory was allocated as you have to copy it anyway and destroy the old copy (some possible optimizations notwithstanding).

The data in the vector (and all other STL containers) is moved around by value as well, so you store the copy of your integers, not pointers to them. That means your objects can be copied around several times in any container operation and they must implement a copy constructor and/or assignment operator correctly. C++ generates those for you by default (just calling copy ctor on all your member variables), but they don't always do the right thing.

If you want to store pointers in an STL container consider using shared pointer wrappers (std::shared_ptr or boost::shared_ptr). They will ensure the memory is handled correctly.

like image 63
Eugene Avatar answered Oct 04 '22 20:10

Eugene


Yes, it will work normally because the memory for the elements are allocated and that is what will be used to build the vector<int> A = variable. However, performance wise, it is not the best idea.

I would suggest changing your function to be the following though

void initVector(vector<int>& a, int size) 

For additional references on usage, please see Returning a STL vector from a function… and [C++] Returning Vector from Function.

For an additional reference on performance (using C++11), please see Proper way (move semantics) to return a std::vector from function calling in C++0x

like image 24
josephthomas Avatar answered Oct 04 '22 20:10

josephthomas