本文介绍了新的和删除,请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的char [],当我尝试删除它时,调试给出了这个错误(在

运行时):


调试错误!$ / $

程序:xxx


损坏:在0x00C315A0正常块(#58)之后。

我得到这个错误到处都是我使用delete []。当我从我的代码中删除所有delete []'时,我没有得到错误(程序有时也没有在发行版中崩溃

编译),但我不能这样做(内存泄漏)。


这是我的代码的一部分:


** BEGIN **


int a = 0;

char * name = NULL;

a = GetWindowTextLength(GetDlgItem(:: hwnd, IDC_NAME))+ 1;

if(!(name = new char [a]))

{

MessageBox(:: hwnd, 没有足够的内存来分配

值!,NULL,MB_OK | MB_ICONERROR);

返回;

}

GetDlgItemText(:: hwnd,IDC_NAME,name,a);

//这给出了错误:

删除[]名称;


**结束**


当我检查名称值时,它确实给出了预期值!


我顺便使用MSVC ++ 6.0。


我是编程C ++的新手,非常感谢任何帮助,我真的可以'$

找出这一个...


-


问候,

Spikinsson

I create a new char[] and when I attempt to delete it, the debug gives this error (during
runtime):

Debug Error!

Program: xxx

DAMAGE: after Normal block (#58) at 0x00C315A0.
I get this error everywhere I use delete []. When I remove all delete []''s from my code I
don''t get the errors (nor does the program sometimes crash in release version
compilation), but I can''t do that can I (memory leaks).

This is a part of my code:

** BEGIN **

int a = 0;
char *name = NULL;
a = GetWindowTextLength(GetDlgItem(::hwnd, IDC_NAME)) + 1;
if (!(name = new char[a]))
{
MessageBox(::hwnd,"Not enough memory to allocate the
value!",NULL,MB_OK|MB_ICONERROR);
return;
}
GetDlgItemText(::hwnd, IDC_NAME, name, a);
// THIS GIVES THE ERROR:
delete [] name;

** END **

When I check the name value, it does give the expected value!

I use MSVC++ 6.0 by the way.

I am pretty new at programming C++ and any help is very much appreciated, I really can''t
figure out this one...

--

Regards,
Spikinsson

推荐答案




希望这只写a而不是a。值得称赞。


删除爆炸的常见原因是:


1.溢出分配。

2.删除未被新分配的东西
3.删除两次。


这不一定是你要删除的东西这就是问题。

有时它会被其他一些分配搞砸了。



Hopefully, this writes no more than "a" chars to name.

The usual reasons delete blows up is because:

1. Overflow the allocation.
2. Delete something not alloc''d by new
3. Delete something twice.

It''s not necessarily the thing that you are deleting that is the problem.
Sometimes it''s some other allocation that was messed up.




感谢您的快速回复,我不太明白您在这里说的是什么

实际上......


Thanks for your quick response, I don''t quite understand what you''re saying here
actually...



希望这只写a而不是a。 chars to name。



Hopefully, this writes no more than "a" chars to name.



定义GetDlgItemText表示写的最大字符是a。

删除爆炸的常见原因是:

1.溢出分配。
我不知道这是什么......

2.删除不被新
分配的东西因为代码显示它被分配了新的。

3.删除两次。
很确定不是这种情况


这不一定是你要删除的问题。
有时它是'其他一些分配搞砸了。


The definition GetDlgItemText says that the maximum characters written is a.

The usual reasons delete blows up is because:

1. Overflow the allocation.I don''t know what this is...
2. Delete something not alloc''d by newAs the code shows it is alloc''d by new.
3. Delete something twice.Quite certain that this is not the case

It''s not necessarily the thing that you are deleting that is the problem.
Sometimes it''s some other allocation that was messed up.



如果我用2个消息框包围delete [],我会得到第一个消息框,然后是

错误,比起第二个消息框,所以我认为应该是错误的...


If I surround the delete [] with 2 message boxes, I get the first message box, then the
error, than the second message box, so I figure that should be the erro...




这对于像VC ++ 6这样破碎的编译器只能为零。



This can only be zero on broken compilers like VC++ 6.


感谢您的快速回复,我不太明白您在这说什么
实际上......


Thanks for your quick response, I don''t quite understand what you''re saying here
actually...




标准C ++将返回成功分配或抛出bad_alloc异常。

在上面的情况下,它永远不会返回null。



Standard C++ will either return the successful allocation or throw a bad_alloc exception.
It will NEVER return null in the case above.


我不知道这是什么...


I don''t know what this is...




在字符外写字返回(通常是在结束时,但在开始时肯定会导致崩溃)。



Write outside the characters returned (usually off the end, but off the beginning is sure to cause a crash).


如果我是使用2个消息框围绕delete [],我得到第一个消息框,然后是第二个消息框的
错误,所以我认为应该是错误...


If I surround the delete [] with 2 message boxes, I get the first message box, then the
error, than the second message box, so I figure that should be the erro...



不一定。请考虑以下事项:


char * a = new char [10];

char * b = new char [10];

memset(b,0,100); //过度分配b。


MESSAGE_BOX

delete [] a; // BOOM!

MESSAGE_BOX


很可能发生了一些错误的内存写入或其他滥用新/删除的行为。

这只是第一次使用新的/删除后发生的错误让你感到困惑。


为什么不提供一个完整的最小程序来证明你的问题。另一个

选项是查看编译器提供的内容以帮助诊断。调试中的VC ++

模式在文档中确实有一些东西(查找mallocdbg或dbgmalloc,我不记得了)。


Not necessarily. Consider the following:

char* a = new char[10];
char* b = new char[10];
memset(b, 0, 100); // overlow the allocation of b.

MESSAGE_BOX
delete [] a; // BOOM!
MESSAGE_BOX

It''s quite possible that some errant memory write or other abuse of new/delete has occurred.
It''s just the first use of new/delete AFTER that error has occured that is hanging you.

Why not provide a complete minimal program that demonstrates your problem. The other
option is to look at what your compiler provides to help diagnose there. VC++ in debug
mode does have some stuff (look up mallocdbg or dbgmalloc, I don''t remember) in the docs.


这篇关于新的和删除,请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:08