Home > Enterprise >  Global vector data disappearing when executing another function
Global vector data disappearing when executing another function

Time:09-18

I am facing a problem with global a vector. I actually have a vector of vectors of pairs, and I declared it as a global variable.

vector<vector<pair<int, float>>> numbers;

In the main function I do some push_backs passing a vector os pairs as an argument, which works just fine.

numbers.push_back(VectorOfPairs);

The problem comes out when I call a function that uses numbers, which is my vector of vectors. For some reason, all the content that I stored in the vector gets empty for no reason. I tried to debbug, and I saw that in function main the size of the vector is actually right, but when I call some function that uses the numbers vector, the size changes from any number to 0.

vector<vector<pair<int, float>>> numbers;
//vector declaration 

//suppose I add some elements in the vector in function **main**
//printing the size of the vector works just fine


void matrixVectorMult(){

//if I call this function right after and try to print the size again, it prints **0**.

    printf("-%d-", numbers.size());
    
}

I would appreciate any help or hints about how to solve this problem.

https://pastebin.com/vkNNk6Ls

that's my code.

CodePudding user response:

you have a variable called numbers in main, and you pass this as an argument to those functions. That 'numbers' has nothing to do with the global valiable of the same name.

  • Related