Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Global vector data disappearing when executing another function

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);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading