hello, I am a beginner i C++. I have been passing for some troubles here, my code gets the warning of exceeds/analysis stacksize, the error C6262 in visual studio. How to reduce the memory usage? My code is correct? Please do not hesitate in tell me my mistakes! I am aiming to put the data from the .txt into a variable matrix to do posterior calculus with it.
To correct the problem behind this warning, you can either move some data to the heap or to other dynamic memory.
An easy -- and safe -- way to store the data on the heap is using a std::vector. std::vector stores its data on the heap automatically when using the default allocator.
1. Add #include <vector> to your includes.
2. Change the 2D array definitions at lines 7 & 23 to: std::vector<std::vector<float>> distances_1(1024, std::vector<float>(1024, 0));std::vector<std::vector<float>> distances_2(1024, std::vector<float>(1024, 0));
Your 2D containers are zero initialized now. The rest of the code should need no change.
Very interesting, it is for sure a better way to storage a matrix. Thank you so much Furry Guy.
Unfortunately a strange behavior appears when I was checking the values of the elements.
1 2 3 4 5 6 7 8 9 10 11 12
/std::vector<std::vector<float>> result(size, std::vector<float>(size, 0));
int i = 0;
int j=0;
std::cout << distances_1[i][j]; // just to check if the values was read correctly to perform the division below.
//result[i][j] = distances_1[i][j] / distances_2[i][j];
//std::cout <<"Result: "<< result[i][j];
return 0;
}
The std::cout<<distances_1[i][j] only show me the correct value for [0,0], other elements always are printed as zero for some reason.
I think that I found the reason. I tried with other .txt file where the elements a separated by spaces instead of commas, and works correctly. Apparently just do not work with elements separated by commas.
CSV files can cause problems with C++ input streams when using operator>>.
BTW, convention when dealing with 2D container loops has the row loop first, then the column loop second. You have the column loop first, row loop second.
If your input file was:
1 2 3
4 5 6
7 8 9
The 2d container would hold:
1 4 7
2 5 8
3 6 9
Even if your file isn't formatted, no carriage returns, you will have a issue that doesn't keep the data in the same order as in the file.