I am having a memory issue with std:: vector in c++. Here is my code:
#include <iostream> #include <vector> int main () { std::vector< std::vector<float> > mesh_points_A; int N=10; for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ std::vector<float> xyz; xyz.push_back(i); xyz.push_back(j); xyz.push_back(0.3); mesh_points_A.push_back(xyz); } } return 0; } When I increase N to 10000 or higher I am running out of memory... But I think that I am doing something totally wrong, because if I would for example use python with numpy arrays this would be possible easily...
Many thanks in advance.
EDIT: This is the original code. The above code was just a simplification to exemplify the problem better. The question is, if it is somehow possible to create many Surface objects (in the code there are currently two) without running out of memory while keeping N=10000.
// classes example compile with c++ -o Surface Surface.cpp -std=c++11 #include <iostream> #include <vector> #include <array> class Surface { private: std::vector< std::array<float,3> > mesh_points_A; public: float R; float z; // z position if the suface int n_A; //number of area mesh points mesh_points_A.size() Surface(int nxA, float R , float z); }; Surface::Surface(int nxA, float R,float z) : z(z) , R(R) { float dxA= 2*R/(nxA*1.0-1.0); //determine n_A, n_A=0; for(int i=0;i<nxA;i++){ float x = -R+i*dxA; for(int j=0;j<nxA;j++){ float y = -R+j*dxA; if(x*x+y*y<R*R){ n_A+=1; } } } std::cout<<"Number of area mesh points: "<<n_A<<std::endl; mesh_points_A.reserve(n_A); for(int i=0;i<nxA;i++){ float x = -R+i*dxA; for(int j=0;j<nxA;j++){ float y = -R+j*dxA; if(x*x+y*y<R*R){ std::array<float,3> xyz{ {x,y,z} }; mesh_points_A.push_back(xyz); } } } } int main () { int N= 20000; Surface s1(N,0.1,0.0); Surface s2(N,0.1,0.1); return 0; }