我是否必须分配堆上(存储在堆容器内)的所有内容?

发布于 2024-08-19 06:04:05 字数 949 浏览 3 评论 0原文

我正在重写 new 运算符,以便在使用 new 关键字时手动分配堆空间。

通常使用带有指向其项目的指针的堆栈分配的堆容器

CArray<CObject*> objects;

就可以了。但我很挑剔,我想制作缓冲区

CArray<CObject> objects;

,让我可以修改 POD 类型。因此,我将它们分配在自然适合它们使用的堆栈上:

CVertex vertex;
objects.push_back(vertex);

但它破坏了堆(我经历了糟糕的一周之一)并给出了一个疯狂的错误:

0xC0000017: Not Enough Quota.

我预先分配了数组对象,然后使用 = push_back() 内部的运算符。

我通过在堆上分配临时对象然后将它们添加到数组中解决了这个问题。但这似乎不对,我就是不明白。

根据要求,一些代码:

CArray::push_back( T& newElement )
{
m_internalElements[allocatedSize] = newElement;
allocatedSize++;
}

CArray::preallocate_and_initialize( size_t itemCount )
{
T* newInternalElements = mem::allocate_and_initialize( T, itemCount );
//copy over
}

请注意,这一切都适用于堆分配的CVertex,因为我在缓冲区元素的二进制搜索中使用这些对象(在给定的索引缓冲区中查找正确的项目)顶点)并且它构建了一个完美的网格!

I'm overriding the new operator to manually allocate heap space when using the new keyword.

Usually using a stack-allocated heap container with pointers to its items-

CArray<CObject*> objects;

-is fine. but I'm picky and I want to make buffers-

CArray<CObject> objects;

-that let me modify POD types. So, I allocate them on the stack which is naturally suited for their use:

CVertex vertex;
objects.push_back(vertex);

But it corrupts the heap (I've had one of those bad weeks) and gives a crazy error:

0xC0000017: Not Enough Quota.

I pre-allocate the array objects and then use the = operator internally in push_back().

I solved the problem by allocating the temporary objects on the heap and then adding them to the array. But it doesn't seem right, and I just don't get it.

As requested, some code:

CArray::push_back( T& newElement )
{
m_internalElements[allocatedSize] = newElement;
allocatedSize++;
}

CArray::preallocate_and_initialize( size_t itemCount )
{
T* newInternalElements = mem::allocate_and_initialize( T, itemCount );
//copy over
}

Note that it all works with heap allocated CVertex's as I am using these objects in a binary search of the buffer elements (finding the right item in the index buffer for a given vertex) and it builds a mesh perfectly fine!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人疚 2024-08-26 06:04:05

0xC0000017STATUS_NO_MEMORY。即,您耗尽了虚拟地址空间,即您使用了太多内存。

如果您预计有很多元素,那么在重复调用 push_back 之前预留空间应该足以避免内存不足。您可以使用 CArray::SetSize(0, itemCount) 为所有元素预留空间。

0xC0000017 is STATUS_NO_MEMORY. I.e., you exhausted the virtual address space, i.e., you are using too much memory.

If you expect to have a lot of elements, reserving space before you call push_back repeatedly should be enough to avoid running out of memory. You can use CArray::SetSize(0, itemCount) to reserve space for all your elements.

恬淡成诗 2024-08-26 06:04:05

CVertex 是从 CObject 派生的吗?如果 CVertex 有更多实例数据,则无法执行此操作。 (我的意思是你不能创建一个 CObjects 数组并将 CVertexes 放入其中)
编译器应该在 CObject 数组中设置多大的槽,它们将是 CObject 大小,然后你将尝试在槽中放入更大的东西 -> bang

您应该将 boost::shared_ptr 对象放入数组中。一旦你学会了这个成语,你就再也回不去了

CVertex is derived from CObject? You cant do that if CVertex has more instance data. (I mean you cannot create an array of CObjects and put CVertexes in it)
How big should the compiler make the slots in the CObject array, they will be CObject sized, then you will try to put something bigger in the slots -> bang

You should put boost::shared_ptr objects in your array. Once you have that idiom worked out you will never go back

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文