删除矢量**的问题 [英] problem with delete vector**

查看:72
本文介绍了删除矢量**的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

有什么问题?为什么? : - ?



 std :: vector< int> ** indices =  new  std :: vector< int> * [ 8 ]; 
for int i = 0 ; i< 8 ; i ++)
{
indices [i] = new std :: vector< int> [ 8 ];
}

// statments ...

for int i = 0 ; i< 8 ; i ++)
{
// 问题在这里!!!
// 调试断言失败!
// 表达式:_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)
delete indices [i];
}
删除索引;

解决方案

引用:

删除索引[i];



你忘了删除对象数组上的[] ,使用:

  delete  [] indices [i]; 


在我看来,已经分配错了:

索引[i ] = new std :: vector< int> [8]; 



当我正确理解你的代码时,你希望每个单元格索引[i]指向一个std :: vector< int>。所以它应该是:

 indices [i] = new std :: vector< int> ;; 



当然,CPallini是正确的,你应该在最后删除索引时应用数组括号 。但这不是抛出断言的问题。


看起来你想要一个2D数组。我建议改为:



 std :: vector< std :: vector< int>>指标; 

// 陈述......





然后你不必担心内存管理。这是std :: vector的主要目的之一。



如果你想要一个3D阵列,那么:



 std :: vector< std :: vector< std :: vector< int>>>指标; 

// 陈述......


hi to all.
What is the problem? why? :-?

std::vector<int>** indexes = new std::vector<int>*[8];
for(int i = 0; i < 8; i++)
{
    indexes[i] = new std::vector<int>[8];
}

//statments...

for(int i = 0; i < 8; i++)
{
    //problem is here !!!
    //Debug Assertion Failed!
    //expression : _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
    delete indexes[i];
}
delete indexes;

解决方案

Quote:

delete indexes[i];


you forgot to delete[] on the array of objects, use:

delete [] indexes[i];


In my opinion, already the allocation is wrong:

indexes[i] = new std::vector<int>[8];


When I understand your code correctly, you want each cell indexes[i] to point to a std::vector<int>. So it should be:

indexes[i] = new std::vector<int>;


And of course, CPallini is right in that you should apply array brackets when finally deleting indexes. But this is not the problem that threw the assertion.


Looks like you want a 2D array. I would suggest instead something like:

std::vector<std::vector<int>> indexes;

//statements...



and then you don''t need to worry about the memory management. That is one of the main purposes of std::vector.

If you want a 3D array, then:

std::vector<std::vector<std::vector<int>>> indexes;

//statements...


这篇关于删除矢量**的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆