Visual Studio 2005的C0000005错误 [英] C0000005 error for Visual Studio 2005

查看:117
本文介绍了Visual Studio 2005的C0000005错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次问好。我已经设法完成了一个程序,它编译得很好,但是一旦我尝试运行它就崩溃了。调试器声称这是由于未处理的异常c0000005。我在这里找到的唯一一篇文章谈到了使用像strlength这样的C函数的危险,但是我没有在这里使用类似的东西。



我得到一个断点在枚举时它失败



Hello again. I've managed to complete a program, and it compiles fine, but as soon as I try to run it it crashes. The debugger claims this is due to unhandled exception c0000005. The only article I can find on this talks about the dangers of using C functions like strlength, but I'm not using anything like that here.

I get a breakpoint at an enumeration when it fails

enumerated card_status {tapped, untapped, damaged}

class C
{
public:

C()
{
status = tapped;
}

void change_status
{
if (status == tapped)
status = untapped;
};

private:
card_status status;
};



它锁定在status = tapped;每次。有谁知道这里的问题是什么?感谢您的时间。


And it locks up at status = tapped; every time. Does anyone have any idea what the problem here is? Thanks for your time.

推荐答案

C0000005表示访问vialoation。通常,这意味着您正在尝试访问无效内存,方法是取消引用不指向有效内存的指针,或者使用指针超出其指向的内存范围。



在你的情况下,看起来你以某种方式以不恰当的方式调用构造函数 C :: C(),i。即在无效的内存地址上。只有在无效对象或从未使用对象实例化的指针上直接调用构造函数时,才可以执行此操作。错误在调用构造函数的代码中!



PS:

以下是一些可能导致错误C0000005的示例:



1.构造对象的错误方法

C0000005 indicates an access vialoation. Usually it means you are trying to access invalid memory, either by dereferencing a pointer not pointing to valid memory, or by using a pointer to reach past the bounds of the memory it points to.

In your case, it looks like you're somehow invoking the constructor C::C() in an inappropriate way, i. e. on an invalid memory address. This is only possible if you directly call the constructor on an invalidated object, or on a pointer that was never instantiated with an object. The bug is in the code that calls the constructor!

P.S.:
Here are a few examples that may cause error C0000005:

1. wrong way to construct an object
void construct_fail()
{
   C* pc;   // dangerous: declare pointer without initialization!
   pc->C(); // error C0000005
}



2.试图重新初始化被破坏的对象


2. trying to "reinitialize" a destroyed object

void reinitialize_fail(C* pc)
{
   // dangerous: state of pc is unknown at this point - it may be valid or invalid
   pc->C(); // error: if pc is invalid, you get error C0000005, otherwise you construct 
            // an already constructed object, invoking another run-time error
}



3.尝试在无效的内存指针上就地构造对象


3. Trying to construct an object in-place on an invalid memory pointer

C* make_C(void* my_memory_block)
{
   return new (my_memory_block) C; // calls constructor, but fails to check pointer validity
}
...
   void* memblock; // danger: unintialized pointer
   C* pc = make_C(mem); // error C0000005


正如Stefan所说,这将是一个访问卷。



我猜你访问指针C *但它需要指向一个对象:



As Stefan said it will be an access vilation.

I guess you accessing a pointer "C*" but it NEEDS to point to a object:

C *p = new C();
//do stuff with p
delete p;//free memory





如果代码不清楚,你应该提供代码。



You should provide the code if it stays unclear.


这篇关于Visual Studio 2005的C0000005错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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