c++ - How can I free memory for unused elements in 3D vectors? -


i generated 100*100*500 vector (or lets array). fill in elements randomly. elements stay empty. can free memory unused elements. or, vector data structure it?

thank you

you cannot free memory occupied single element inside array. arrays allocated , freed contiguous memory blocks. might consider storing data inside linked list instead, accomplish functionality. if question saving memory, sparse vectors come mind.

edit: have clarified (in comment section) aim store , graphically represent given set of 3d data, can come more detailed answer:

a commonly used way sparsely store 3d data octree. use kind of voxel-engine, octree implemented this:

enum atomtype {     notype,     solidtype,     strangetype };  class octreenode {     public:         virtual octreenode* getsubnode(unsigned index) = 0;         virtual atomtype getcontent(void) = 0; };  class octreebranchnode : public octreenode {     public:         octreenode* getsubnode(unsigned index)         {             if (subnodes) return subnodes[index]; else return nullptr;         }          atomtype getcontent(void) { return notype; }      private:         octreenode** subnodes; };  class octreeatomnode : public octreenode {     public:         octreenode* getsubnode(unsigned index) { return nullptr; }          atomtype getcontent(void) { return content; }      private:         atomtype content; }; 

the point is, don't need store coordinates way, because 3d-position of each node clear position inside tree-hierarchy.

if want render data set, might want try out marching cubes or render every atom cubic box (doesn't matter if data set large enough , atoms pretty small). there nice tutorial on how write voxel engine based on opengl.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -