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

I know that the variables allocated in this function stack become inaccessible when the function completes execution. However, vector types allocate their elements on the heap no matter how they are distributed. For example,

vector<int> A; 

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

My question is: suppose 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 issues when using vector A in the main function? I tried this several times and they all worked fine, but I'm afraid it might just be luck.

As I see it, vector A allocates its elements on the heap, but it has some "utility" parameters (possibly the size of the vector) allocated on the stack itself. Therefore, the use of a vector in the main function can lead to a memory access problem if these parameters are overwritten by another distribution. Any ideas?

+7
source share
3 answers

Yes, it will work fine, because memory is allocated for the elements, and this will be used to create the variable vector<int> A = . However, performance is wise; this is not a good idea.

I would suggest changing your function as follows though

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

For additional usage references, see Returning an STL Vector from a Function ... and [C ++] Returning a Vector from a Function .

For additional help on performance (using C ++ 11), please see the Correct way (moving semantics) to return std :: vector from a function calling in C ++ 0x

+1
source

When you do "return A;" you return by value, so you get a copy of the vector - C ++ creates a new instance and calls the copy constructor or operator = on it. Therefore, in this case, it does not matter where the memory was allocated, since you still need to copy it and destroy the old copy (despite some possible optimizations).

Data in the vector (and all other STL containers) also moves by value, so you save a copy of your integers, not pointers to them. This means that your objects can be copied several times in any operation with the container, and they must correctly implement the copy constructor and / or assignment operator. C ++ generates a default for you (just by invoking ctor to copy all of your member variables), but they don't always do the right thing.

If you want to store pointers in an STL container, consider using the covers of shared pointers (std :: shared_ptr or boost :: shared_ptr). They will ensure proper memory handling.

+4
source

C ++ vector actually has two parts of memory that are associated with one pointer. The first is on the stack, and the second is on the heap. This way, you have both stack and heap functions in one object.

 std::vector<int> vec; vec.push_back(10); vec.push_back(20); vec.push_back(30); std::cout << sizeof(vec) << std::endl; 

After running this code, you will notice that the stack area contains no elements, but it still exists. Therefore, when you transfer a vector from a function to another, you will need to manipulate the areas of the stack, and the vector will be copied like any other object based on the stack.

0
source

All Articles