我有一个班级指示器声明:
MyClass* a;
在销毁方法方面,我有:
if (a)
{
delete a;
a= NULL;
}
我有一个问题,当删除指针a:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
问题的原因是什么? 我怎样才能摆脱它?
我有一个班级指示器声明:
MyClass* a;
在销毁方法方面,我有:
if (a)
{
delete a;
a= NULL;
}
我有一个问题,当删除指针a:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
问题的原因是什么? 我怎样才能摆脱它?
您的当前声明 :
MyClass* a;
a
得到一个随机值。 如果您以后再不给它一个有效值, 例如 :
a = new MyClass();
它会指向一个未知的记忆位置, 很可能不是为您的程序保留的记忆区域, 因此当您试图删除它时会出错 。
避免这一问题的最简单方式是当您声明 a
值时给予该值 :
MyClass* a = new MyClass();
(如果你们)申报的时候,不能给它一个数值,(如果你们不知道它),或以无效的数值分配它:
MyClass* a = 0;
顺便说一下,您可以从您的代码中删除测试 (if (a)
)。 delete
是空指针上的无操作。
使用智能指针到自由内存。 应用程序代码中 < code> delete code> 总是错误 。
除非您已经初始化了指针到此之后的某个地方:
MyClass* a;
指针 a
将有一些随机值。 因此, 您的测试
if (a) { }
将会通过, 您试图删除一些随机的内存位置 。
您可以通过初始化指针来避免此选项 :
MyClass* a = 0;
其他选项是,指向的对象已被删除到别处,指针没有设置为 0
,或者指向堆栈上分配的对象。
正如在其他地方所指出的,您可以使用智能指针来避免所有这些麻烦,而不是首先使用光指针。我建议查看。
您如何分配 a
指点的内存? 如果您使用 new[] <
(为了创建 myClass
的阵列), 您必须用 myClass code > a; code> 进行处理。 如果您用
(在与类打交道时可能是一个坏主意) 来分配内存? 您必须用 malloc ()
() free ()
进行处理。
如果您用 new
来分配内存, 您可能会在其他地方造成内存管理错误, 例如, 您可能已经交易了 < code> a code >, 或者您已经在某些数组的界限之外写了字。 请尝试使用 Valgrind 来调试内存问题 。
你应该使用
MyClass* a = NULL;
在您的声明中。 如果您从不即时输入一个, 指针指向一个未定义的内存区域。 当包含类“ 毁灭者” 执行时, 它试图将该随机位置 < code> delete 。
When you do MyClass* a;
you declare a pointer without allocating any memory. You don t initialize it, and a
is not necessarily NULL. So when you try to delete it, your test if (a)
succeeds, but deallocation fails.
You should do MyClass* a = NULL;
or MyClass* a(nullptr);
if you can use C++11.
(I assume here you don t use new
anywhere in this case, since you tell us that you only declare a pointer.)
I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...
I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?