C++ 堆分配/解除分配导致堆栈溢出

C++ 堆分配/解除分配导致堆栈溢出,c++,visual-studio-2005,memory-management,stack-overflow,C++,Visual Studio 2005,Memory Management,Stack Overflow,编辑:为了澄清问题,这个问题是由我的代码输入错误引起的, 在 应该是 pointer = new BYTE[datasize]; 一切都很好 结束 嗨 我在C++项目中VisualStudio 2005中有一个奇怪的堆栈溢出问题。 在我的代码中,我有一个 BYTE* pointer; 此指针设置为NULL,然后分配一些内存,然后清除为0x00。像这样: pointer = NULL; pointer = new BYTE(dataSize); memset(pointer,0x00,data

编辑:为了澄清问题,这个问题是由我的代码输入错误引起的, 在

应该是

pointer = new BYTE[datasize];
一切都很好

结束

我在C++项目中VisualStudio 2005中有一个奇怪的堆栈溢出问题。

在我的代码中,我有一个

BYTE* pointer;
此指针设置为NULL,然后分配一些内存,然后清除为0x00。像这样:

pointer = NULL;
pointer = new BYTE(dataSize);
memset(pointer,0x00,dataSize);
现在,我运行了几次,得到了两个不同的结果。。有时(在程序的后面部分,当我使用delete[]删除指针时),它会说堆已损坏,并且在检查调用堆栈时,似乎_CrtIsValidHeapPointer会断言它不是有效的指针。然而,我检查了这个指针,它对我来说似乎是有效的(它有一个内存地址)。我错过什么了吗

在另一个场景中,应用程序冻结很短时间,我收到一条堆栈溢出消息。当我检查调用堆栈时,它看起来是这样的

 something.dll!_heap_alloc_base(unsigned int size=568)  Line 105 + 0x28 bytes   C
something.dll!_heap_alloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 411 + 0x9 bytes C++
something.dll!_nh_malloc_dbg(unsigned int nSize=532, int nhFlag=0, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 268 + 0x15 bytes   C++
something.dll!_malloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 191 + 0x1b bytes    C++
something.dll!_calloc_dbg(unsigned int nNum=1, unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 563 + 0x15 bytes   C++
something.dll!_getptd_noexit()  Line 608 + 0x18 bytes   C
something.dll!_errno()  Line 281 + 0x5 bytes    C
something.dll!_heap_alloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 415 + 0x5 bytes C++
something.dll!_nh_malloc_dbg(unsigned int nSize=532, int nhFlag=0, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 268 + 0x15 bytes   C++
something.dll!_malloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 191 + 0x1b bytes    C++
something.dll!_calloc_dbg(unsigned int nNum=1, unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 563 + 0x15 bytes   C++
something.dll!_getptd_noexit()  Line 608 + 0x18 bytes   C
something.dll!_errno()  Line 281 + 0x5 bytes    C
something.dll!_heap_alloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 415 + 0x5 bytes C++
something.dll!_nh_malloc_dbg(unsigned int nSize=532, int nhFlag=0, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 268 + 0x15 bytes   C++
something.dll!_malloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 191 + 0x1b bytes    C++
something.dll!_calloc_dbg(unsigned int nNum=1, unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 563 + 0x15 bytes   C++
something.dll!_getptd_noexit()  Line 608 + 0x18 bytes   C
something.dll!_errno()  Line 281 + 0x5 bytes    C
something.dll!_heap_alloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 415 + 0x5 bytes C++
something.dll!_nh_malloc_dbg(unsigned int nSize=532, int nhFlag=0, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 268 + 0x15 bytes   C++
something.dll!_malloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 191 + 0x1b bytes    C++
something.dll!_calloc_dbg(unsigned int nNum=1, unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 563 + 0x15 bytes   C++
something.dll!_getptd_noexit()  Line 608 + 0x18 bytes   C
something.dll!_errno()  Line 281 + 0x5 bytes    C
有很多这样的调用,我很确定这是导致堆栈溢出的原因。。知道这个问题是什么吗?我试着查看我是否分配/取消分配错误,但我没有写入除了memset之外分配的内存块

我分配/释放内存的方式是否有问题

我正在使用此代码取消分配:

if (pointer != NULL){
    delete[] pointer;
    pointer = NULL;
}
这应该确保我没有释放已经释放的内存,对吗

谢谢

编辑:在“输出”窗口中,我还得到了大量以下信息:

First-chance exception at 0x76df0839 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e2871f in app.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
我想你的意思是:

pointer = new BYTE[size];
我想你的意思是:

pointer = new BYTE[size];

您的取消分配代码应为:

if (pointer != NULL){
    delete[] pointer;
    pointer = NULL;
}

您的取消分配代码应为:

if (pointer != NULL){
    delete[] pointer;
    pointer = NULL;
}

不,空测试是不必要的。是的,我只是回应了OP中的一个输入错误:))是的。该标准规定,尝试删除
null
等同于什么都不做。不,没有必要进行null测试。是的,我只是对OP:)中的一个输入错误做出了回应。是的。该标准规定,试图删除
null
等同于什么都不做。哦,我明白了,
new byte(size)
new byte[size]
将分配相同的内存量,但您必须
删除
前者和
删除[]
后者。@Igor:error
新字节(size)
将分配一个字节,并使用值
size
对其进行初始化
new byte[size]
将创建
size
字节,而无需默认初始化。哦,我明白了,
new byte(size)
new byte[size]
将分配相同的内存量,但您必须
删除
前者和
删除[]
后者。@Igor:不正确
新字节(size)
将分配一个字节,并使用值
size
对其进行初始化
newbyte[size]
将创建
size
字节,无默认初始化。