保留和删除向量?
我在删除向量数据时遇到问题。
当我创建数据时,我首先在数组中保留空间,然后调整向量的大小并复制数组的地址:
//Create the vertices
verts.reserve(vn); verts.resize(vn);
TriVertex *vertsaux = new TriVertex[vn];
for(int i=0, c=0; i<vn; i++, c+=3)
{
vertsaux[i].SetId(i);
vertsaux[i].SetCoords0(Vector3(vs[c], vs[c+1], vs[c+2]));
//Inicializate texture vertices
vertsaux[i].SetTextureCoords(Vector2(0.0f, 0.0f));
}
for(int i=0; i<vn; i++)
{
verts[i] = &vertsaux[i];
}
但是在我的类的析构函数中,当我这样做时,它会给我一个运行时错误:
for (i=0; i < this->verts.size(); i++) {
delete this->verts[i];
}
任何人都知道为什么可以这正在发生吗?
顺便说一句,由于一些实现细节,我不能只在 for
中创建 new TriVertex
...
I'm having a problem deleting data of a vector.
When I create the data, I first reserve space in an array, and then resize the vector and copy the addresses of the array:
//Create the vertices
verts.reserve(vn); verts.resize(vn);
TriVertex *vertsaux = new TriVertex[vn];
for(int i=0, c=0; i<vn; i++, c+=3)
{
vertsaux[i].SetId(i);
vertsaux[i].SetCoords0(Vector3(vs[c], vs[c+1], vs[c+2]));
//Inicializate texture vertices
vertsaux[i].SetTextureCoords(Vector2(0.0f, 0.0f));
}
for(int i=0; i<vn; i++)
{
verts[i] = &vertsaux[i];
}
But in the destructor of my class, it gives me a runtime error when I do this:
for (i=0; i < this->verts.size(); i++) {
delete this->verts[i];
}
Anybody know why can this is happening?
By the way, I can't just create new TriVertex
inside the for
, because of some implementation details...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您分配了一个对象数组,
要再次删除该对象,您需要执行以下操作:
您不能单独删除元素。
You allocate an array of objects with
To delete that again you need to do
You can not delete the elements individually.
您并没有真正向我们提供足够的信息。它是什么
顶点
的类型?为什么要使用versaux
?从什么我几乎看不到,最合乎逻辑的解决方案是
std::vectorverts;
作为类成员,并且直接初始化,无需中间动态
分配。也许:
我没有看到任何中间动态的原因
分配的数组。 (如果有的话,乔的回答是正确的。)
You're not really giving us enough information. What it the
type of
verts
? And why are you usingversaux
? From whatlittle I can see, the most logical solution would be to have
std::vector <TriVertex> verts;
as a class member, andinitialize it directly, without the intermediate dynamic
allocations. Maybe:
I don't see any reason for the intermediate, dynamically
allocated array. (If there is, Joe's response is correct.)
当您使用分配数组时,
必须通过调用使用
delete[]
来取消分配When you allocate an array using
you have to deallocate using
delete[]
by calling由于您创建了一个由
vertsaux
指向的TriVertex
数组,因此您应该使用以下方法删除该数组:您无法删除数组中的各个元素。
完成上述
delete[]
后,您应该像这样删除verts
的所有元素:这是因为自从您删除了
vertsaux
后,verts
内的指针无效。我假设 verts 类似于:
std::vector
Since you create an array of
TriVertex
, pointed to byvertsaux
, then you should delete the array using:You can't delete the individual elements of the array.
Having done the above
delete[]
, you should remove all the elements ofverts
like this:This is because since you have deleted
vertsaux
, the pointers insideverts
are invalid.I'm assuming that verts is something like:
std::vector<TriVertex*>
这会删除您创建的动态分配数组中的一项,实际上是 UB 以这种方式删除它。
像这样的数组应该使用
delete[]
语句删除,这会删除整个数组。This deletes one item inside the dynamically allocated array you created and is actually UB to delete it that way.
An array like that should be deleted with
delete[]
statement, and that deletes the entire array.这里有两件事:
正如许多人指出的那样,分配数组需要将其作为数组删除。然而,我认为它掩盖了你真正想要在这里做的事情。更改您的分配,为每个项目执行“
new TriVertex
”。假设您的向量正在存储
TriVertex*
指针,您确实必须删除析构函数中的每个项目(如您所写)。因此,修复
TriVertex
对象的初始分配策略应该可以解决问题。There are two things here:
As a number of people have pointed out, allocating an array requires deleting as an array. However, I think it's masking what you're really trying to do here. Change your allocation to do a "
new TriVertex
" for each item.Assuming your vector is storing
TriVertex*
pointers, you indeed have to delete each item in your destructor (as you have written.)Thus, fixing your initial allocation strategy for
TriVertex
objects should solve the problem.