简体   繁体   中英

Return 2D vector using pointer in C++

all

I am using a 2D vector to return my results from a function to main. The 2D vector is very large, and the function is excuted 10K+ times, thus I have to return it using pointer to speed it up.

Here is my code:

vector<vector<double>>* someFunction()
{
    vector<vector<double>> results;
    //do something...
    return &results;
}

and in main:

int main()
{
    // do something
    ////////// EDIT: vec is a pointer, I missed * at first place
    vector<vector<double>>* vec = someFunction();    // vec is empty!

    cout<<vec.size();        // size = 0

    return;
}

When I check the value and size of vec in main, it turned out that vec is size 0.

May I know what am I wrong?

Many thanks in advance.

Regards

Long

You are returning a pointer to a local variable. This variable ceases to exist when the function scope is exited, leaving you with a dangling pointer. This is undefined behaviour .

Just return a value:

vector<vector<double>> someFunction()
{
    vector<vector<double>> results;
    //do something...
    return results;
}

If you are worried about expensive copies involved in returning by value, bear in mind that it is extremely likely that these will be elided via return value optimization (RVO), specifically named return value optimization (NRVO). Even if this doesn't happen (it is optional, so implementations can decide not to do it), in C++11 move semantics would kick in. Moving an std::vector<double> is extremely cheap.

Are you not returning a reference to a local variable here, therefore going out of scope? Make it a member of the class, assuming its in a class

Alternatively pass the reference into the function as a parameter

void someFunction(vector<vector<double>>& results)
{
    results.clear();

    //do something...
}    

you return a pointer to vector. remove the * in the method:

vector<vector<double>> someFunction()

and of course return the vector, not a pointer to it

There appears already to be a popular answer, but there is a way to do exactly what your asking using dynamic memory allocation. I feel like this is worth pointing out since, although it may not be the best way to approach this particular problem, dynamic memory allocation is a very useful technique in general. The idea is that "someFunction" should create a pointer to a dynamically allocated piece of memory which will hold the vector<vector<double>> . Here is working code using smart pointers but one could instead use the traditional new and delete keywords for allocation also if c++11 is not your thing:

#include <memory>
#include <vector>

using namespace std;

shared_ptr<vector<vector<double>>> someFunction() {
  vector<vector<double>> result;
  //Do stuff with result                                                                        
  shared_ptr<vector<vector<double>>> resultPtr =
    make_shared<vector<vector<double>>>(result);
  return resultPtr;
}

int main() {
  shared_ptr<vector<vector<double>>> ptr;
  ptr = someFunction();
  return 0;
}

Like I said it may not be the best way, but let it be known that returning a pointer to a local object can indeed be done correctly using dynamic memory allocation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM