无法删除数组(我不知道的错误)
Trouble deleting arrays (an error unknown to me)
我有一个应该 "breathe" 正在使用的结构。它是一个指针矩阵。
(BigInt 是某种类型,不管它是什么...)
BigInt ***directory;
这样初始化(矩阵大小为M*M):
directory = new BigInt**[M];
for(int i=0;i<M;i++)
directory[i] = NULL;
并在需要时以这种方式展开:
partition = ...;
directory[partition] = new BigInt*[M];
for(int i=0;i<M;i++)
directory[partition][i] = NULL;
并以这种方式销毁(此方法从具有 BigInt*** 目录作为字段的 class 的析构函数中调用):
void del() {
for(int p=0;p<M;p++)
if(directory[p]!=NULL) {
for(int o=0;o<M;o++)
if(directory[p][o]!=NULL)
delete directory[p][o];
}
for(int p=0;p<M;p++)
if(directory[p]!=NULL)
delete directory[p];
delete directory;
}
然而,在我的程序结束时,我的程序在 dbgheap.c 中断(触发断点),地址:
/***
*int _CrtIsValidHeapPointer() - verify pointer is from 'local' heap
*
*Purpose:
* Verify pointer is not only a valid pointer but also that it is from
* the 'local' heap. Pointers from another copy of the C runtime (even in the
* same process) will be caught.
*
*Entry:
* const void * pUserData - pointer of interest
*
*Return:
* TRUE - if valid and from local heap
* FALSE otherwise
*
*******************************************************************************/
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;
if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;
return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}
当我尝试通过调用 del() 释放内存或当我尝试删除单个数组(当矩阵为 "breathing" 时)时会出现相同的断点,如下所示:
int p = ...;
delete directory[p];
我从来没有发生过这种错误,如果我不释放我的内存,程序运行正常。
您正在使用 new[] 分配数组,但使用 delete 运算符删除它们。如果你使用 new[] 分配一些东西,你应该使用 delete[] 删除它,否则你会触发未定义的行为。
例如,您应该替换此代码:
delete directory[p];
有了这个:
delete[] directory[p];
这同样适用于您发布的代码中出现的所有其他 delete。
我有一个应该 "breathe" 正在使用的结构。它是一个指针矩阵。 (BigInt 是某种类型,不管它是什么...)
BigInt ***directory;
这样初始化(矩阵大小为M*M):
directory = new BigInt**[M];
for(int i=0;i<M;i++)
directory[i] = NULL;
并在需要时以这种方式展开:
partition = ...;
directory[partition] = new BigInt*[M];
for(int i=0;i<M;i++)
directory[partition][i] = NULL;
并以这种方式销毁(此方法从具有 BigInt*** 目录作为字段的 class 的析构函数中调用):
void del() {
for(int p=0;p<M;p++)
if(directory[p]!=NULL) {
for(int o=0;o<M;o++)
if(directory[p][o]!=NULL)
delete directory[p][o];
}
for(int p=0;p<M;p++)
if(directory[p]!=NULL)
delete directory[p];
delete directory;
}
然而,在我的程序结束时,我的程序在 dbgheap.c 中断(触发断点),地址:
/***
*int _CrtIsValidHeapPointer() - verify pointer is from 'local' heap
*
*Purpose:
* Verify pointer is not only a valid pointer but also that it is from
* the 'local' heap. Pointers from another copy of the C runtime (even in the
* same process) will be caught.
*
*Entry:
* const void * pUserData - pointer of interest
*
*Return:
* TRUE - if valid and from local heap
* FALSE otherwise
*
*******************************************************************************/
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;
if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;
return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}
当我尝试通过调用 del() 释放内存或当我尝试删除单个数组(当矩阵为 "breathing" 时)时会出现相同的断点,如下所示:
int p = ...;
delete directory[p];
我从来没有发生过这种错误,如果我不释放我的内存,程序运行正常。
您正在使用 new[] 分配数组,但使用 delete 运算符删除它们。如果你使用 new[] 分配一些东西,你应该使用 delete[] 删除它,否则你会触发未定义的行为。
例如,您应该替换此代码:
delete directory[p];
有了这个:
delete[] directory[p];
这同样适用于您发布的代码中出现的所有其他 delete。