简体   繁体   中英

How do I erase an element of vector without application crashing due to lack of memory?

So this problem is kind of hard to explain in a title so here it goes :

In my 32-bit application, I have a vector of point cloud that is for example, of size 4000. Let's say there is 20 000 points in each element of point cloud.

I wish to be able to delete a point cloud depending on whatever my condition is.

The problem is that, I am almost already hitting the Memory usage limit with my 4000 elements ( if for example I had 4500, my application would crash).

If I delete a point cloud when my vector size is 4000, it crashes, if it is a smaller size (lets say around 2000) it does not crash.

My theory : When erasing it, it re-allocates the vector somewhere else, but by doing so when I am near the memory limit, it hits the memory cap and crashes my app ?

Is there anyway to ensure the vector stays in that 'location' and not make my app crash or any other solution ?

Thanks in advance!

I'm not sure about deleting vector element but I want to suggest you to change the elements order and not do reallocation. I think that reorganize the vector will cost you less then reallocate new memory, and in the end of the vector you can mark the empty places with NULL or holding var that will represent the real size of the vector (in your case after deleting the point the real size will be 3999). That way your allocation is done only once and you don't need to work with the OS allocator anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM