如何删除动态矢量数组? [英] How to delete a dynamic array of vectors?

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

问题描述

我需要以下代码的帮助,它会抛出访问冲突分配错误:

I need help with the code below, it throws an access violation allocation error:

std::vector<void**> *ut = new std::vector<void**>[0xFFFFFF + 1]; // array needs to be allocated on the heap

....

std::vector<void**>::iterator it;
for(unsigned int i = 0; i < 0xFFFFFF + 1; i++) {
	for(it = ut[i].begin(); it != ut[i].end(); ++it) { // <- access violation reading error
		ut_delete(*it); // free dynamically allocated memory pointed by vector elements
	}
}

if(ut != NULL) delete[] ut;

谢谢!

推荐答案

On 7 / 21:30 12:40 PM,alikim写道:

On 7/21/2017 12:40 PM, alikim wrote:


[code]

std :: vector< void **> * ut = new std :: vector< void **> [0xFFFFFF + 1]; //数组需要在堆上分配

[code]
std::vector<void**> *ut = new std::vector<void**>[0xFFFFFF + 1]; // array needs to be allocated on the heap

....

这些点掩盖的代码中可能存在问题。

The problem likely likes in the code masked by those dots.


std :: vector< void **> :: iterator it;

for(unsigned int i = 0; i< 0xFFFFFF + 1; i ++){

  for(it = ut [i] .begin(); it!= ut [i] .end(); ++ it){//< - 访问冲突读取错误

  &NBSP; ut_delete(*它); //由向量元素指向的自由动态分配的内存

std::vector<void**>::iterator it;
for(unsigned int i = 0; i < 0xFFFFFF + 1; i++) {
  for(it = ut[i].begin(); it != ut[i].end(); ++it) { // <- access violation reading error
    ut_delete(*it); // free dynamically allocated memory pointed by vector elements

在哪里以及如何分配该内存? ut_delete()究竟做了什么?

Where and how are you allocating that memory? What exactly does ut_delete() do?


if(ut!= NULL)delete [] ut;

if(ut != NULL) delete[] ut;

在这里检查ut是否为时已晚。在那之前你已经多次取消引用了它。如果它实际上是NULL,那么你的程序将无法到达 - 它会更早崩溃。也许这正是发生的事情。

It's a bit too late to check ut for NULL here. You've dereferenced it plenty of times before that. If it's in fact NULL, your program won't get here - it'll crash much earlier. Perhaps that's exactly what happens.


这篇关于如何删除动态矢量数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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