简体   繁体   English

如何在不因内存不足而导致应用程序崩溃的情况下擦除矢量元素?

[英]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.在我的 32 位应用程序中,我有一个点云向量,例如,大小为 4000。假设点云的每个元素中有 20 000 个点。

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).问题是,我的 4000 个元素几乎已经达到内存使用限制(例如,如果我有 4500 个元素,我的应用程序就会崩溃)。

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.如果我在矢量大小为 4000 时删除点云,它会崩溃,如果它的大小较小(比如 2000 左右),它不会崩溃。

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).我认为重新组织向量将花费更少然后重新分配新内存,并且在向量的末尾,您可以用 NULL 标记空位或持有表示向量实际大小的 var(在您的情况下删除点后)实际大小为 3999)。 That way your allocation is done only once and you don't need to work with the OS allocator anymore.这样你的分配只完成一次,你不需要再使用操作系统分配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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