Dynamic memory allocation of vectors in C ++

I need to write a function that adds the elements of two 3D arrays, saves the result in a third 3D array, takes the results and copies them to a one-dimensional vector, and then returns that vector with a permalink. The problem I am facing is how to use dynamic memory allocation to return a vector, since you cannot return a local variable. This is what the code looks like if you CAN return a local variable:

template <class T> const vector<T>& allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width, int depth) { vector<T> results(height*width*depth); // Add the values of array x and y for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { for (int k = 0; k < depth; k++) { z[i][j][k] = x[i][j][k] + y[i][j][k]; } } } int l = 0; //Places the values of array z into a vector for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { for (int k = 0; k < depth; k++) { results[l] = z[i][j][k]; l++; } } } return results; } 

Here is my wrong attempt to use dynamic memory allocation:

 template <class T> const vector<T>& allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width, int depth) { vector<T>* results(height*width*depth) = new vector<T>; // Add the values of array x and y for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { for (int k = 0; k < depth; k++) { z[i][j][k] = x[i][j][k] + y[i][j][k]; } } } int l = 0; //Places the values of array z into a vector for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { for (int k = 0; k < depth; k++) { *results[l] = z[i][j][k]; l++; } } } return *results; } 
+1
source share
2 answers

It makes no sense to return a link to a local variable. If you resort to the second solution (return the link to the vector new 'd), then there will almost certainly be a memory leak. By convention, getting a link usually means that some other object already controls the lifetime of the returned object.

There are many things you can do to get around this problem.


Do not perform dynamic memory allocation; change the function signature.

 // Return a copy of the vector. vector<T> allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width, int depth) { vector<T> results(height*width*depth); // ... return results; } 

If you compiler runs copy elision and / or supports C ++ 11 moving constructors , a "copy" will never take place, and the return will be very efficient.


If you really want to dynamically allocate a vector to satisfy some other constraint, you also need to change the function signature:

 // Return a pointer to a newly allocated vector. const vector<T>* allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width, int depth) { vector<T>* results(height*width*depth) = new vector<T>; // ... return results; } 

If you do, consider returning a smart pointer instead of returning a null pointer.


If it is a member function, perhaps you can save the vector inside the object.

 template<typename T> class SomeClass { std::vector<T> results; public: // ... // Modify member and return reference to internal member. const vector<T>& allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width, int depth) { results.resize(height*width*depth); // ... return results; } }; 

Another possible but very discouraged solution is to return a link to some global variable.

 std::vector<T> results; // Modify global and return reference to global variable. const vector<T>& allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width, int depth) { results.resize(height*width*depth); // ... return results; } 

Or in its disguised (but exactly equivalent) form:

 // Modify global and return reference to global variable. const vector<T>& allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width, int depth) { // Global variable with name not visible outside the function. static std::vector<T> results; results.resize(height*width*depth); // ... return results; } 
+2
source

Use a line vector instead of using malloc / new.

 int t; std::cin >> t; std::vector<std::string> strings(t); //Create t strings for(int i = 0; i < t; i++) std::cin >> strings[i]; //Read into each string 
0
source

All Articles