保留和删除向量?

发布于 2024-11-07 17:53:18 字数 742 浏览 0 评论 0原文

我在删除向量数据时遇到问题。

当我创建数据时,我首先在数组中保留空间,然后调整向量的大小并复制数组的地址:

//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 技术交流群。

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

发布评论

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

评论(6

幸福%小乖 2024-11-14 17:53:18

您分配了一个对象数组,

TriVertex *vertsaux = new TriVertex[vn];

要再次删除该对象,您需要执行以下操作:

delete[] vertsaux;

您不能单独删除元素。

You allocate an array of objects with

TriVertex *vertsaux = new TriVertex[vn];

To delete that again you need to do

delete[] vertsaux;

You can not delete the elements individually.

烧了回忆取暖 2024-11-14 17:53:18

您并没有真正向我们提供足够的信息。它是什么
顶点的类型?为什么要使用 versaux?从什么
我几乎看不到,最合乎逻辑的解决方案是
std::vectorverts; 作为类成员,并且
直接初始化,无需中间动态
分配。也许:

verts.reserve(vn);

for ( int i = 0; i < vn; ++ i ) {
    verts.push_back( TriVertex() );
    TriVertex& current = verts.back();
    current.SetId( i );
    current.SetCoords( Vector3( *vs, *(vs+1), *(vs+ 2) ) );

    current.SetTextureCoords( Vector2( 0.0, 0.0 ) );
    vs += 3;
}

我没有看到任何中间动态的原因
分配的数组。 (如果有的话,乔的回答是正确的。)

You're not really giving us enough information. What it the
type of verts? And why are you using versaux? From what
little I can see, the most logical solution would be to have
std::vector <TriVertex> verts; as a class member, and
initialize it directly, without the intermediate dynamic
allocations. Maybe:

verts.reserve(vn);

for ( int i = 0; i < vn; ++ i ) {
    verts.push_back( TriVertex() );
    TriVertex& current = verts.back();
    current.SetId( i );
    current.SetCoords( Vector3( *vs, *(vs+1), *(vs+ 2) ) );

    current.SetTextureCoords( Vector2( 0.0, 0.0 ) );
    vs += 3;
}

I don't see any reason for the intermediate, dynamically
allocated array. (If there is, Joe's response is correct.)

负佳期 2024-11-14 17:53:18

当您使用分配数组时,

TriVertex *vertsaux = new TriVertex[vn];

必须通过调用使用 delete[] 来取消分配

delete[] vertsaux;

When you allocate an array using

TriVertex *vertsaux = new TriVertex[vn];

you have to deallocate using delete[] by calling

delete[] vertsaux;
仅冇旳回忆 2024-11-14 17:53:18

由于您创建了一个由 vertsaux 指向的 TriVertex 数组,因此您应该使用以下方法删除该数组:

delete[] vertsaux;

您无法删除数组中的各个元素。

完成上述 delete[] 后,您应该像这样删除 verts 的所有元素:

verts.clear();

这是因为自从您删除了 vertsaux 后, verts 内的指针无效。

我假设 verts 类似于:std::vector

Since you create an array of TriVertex, pointed to by vertsaux, then you should delete the array using:

delete[] vertsaux;

You can't delete the individual elements of the array.

Having done the above delete[], you should remove all the elements of verts like this:

verts.clear();

This is because since you have deleted vertsaux, the pointers inside verts are invalid.

I'm assuming that verts is something like: std::vector<TriVertex*>

意犹 2024-11-14 17:53:18
delete this->verts[i];

这会删除您创建的动态分配数组中的一项,实际上是 UB 以这种方式删除它。

像这样的数组应该使用 delete[] 语句删除,这会删除整个数组

delete this->verts[i];

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.

眼泪都笑了 2024-11-14 17:53:18

这里有两件事:

  1. 正如许多人指出的那样,分配数组需要将其作为数组删除。然而,我认为它掩盖了你真正想要在这里做的事情。更改您的分配,为每个项目执行“new TriVertex”。

  2. 假设您的向量正在存储 TriVertex* 指针,您确实必须删除析构函数中的每个项目(如您所写)。

因此,修复 TriVertex 对象的初始分配策略应该可以解决问题。

for(int i=0, c=0; i<vn; i++, c+=3)
{
    TriVertex *vertsaux = new TriVertex;

    vertsaux->SetId(i);
    vertsaux->SetCoords0(Vector3(vs[c], vs[c+1], vs[c+2]));

    //Initializing texture vertices
    vertsaux->SetTextureCoords(Vector2(0.0f, 0.0f));
    verts[i] = vertsaux;
}

There are two things here:

  1. 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.

  2. 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.

for(int i=0, c=0; i<vn; i++, c+=3)
{
    TriVertex *vertsaux = new TriVertex;

    vertsaux->SetId(i);
    vertsaux->SetCoords0(Vector3(vs[c], vs[c+1], vs[c+2]));

    //Initializing texture vertices
    vertsaux->SetTextureCoords(Vector2(0.0f, 0.0f));
    verts[i] = vertsaux;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文