C++ 0xC0000005:访问冲突读取位置0x00000000
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10478941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
0xC0000005: Access violation reading location 0x00000000
提问by Milk
I'm having a very strange issue with a space invaders game I'm working on. Basically I get an access violation error:
我正在开发的太空入侵者游戏有一个非常奇怪的问题。基本上我得到一个访问冲突错误:
Unhandled exception at 0x5edad442 (msvcr100d.dll) in SpaceInvaders.exe: 0xC0000005: Access violation reading location 0x00000000.
SpaceInvaders.exe 中 0x5edad442 (msvcr100d.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000000。
when I include the piece of code below. visual studio takes me to "strcmp.asm" when debugging. Note that Im not using strcmp() in any of my code. Is there anything wrong with the code, or is this a problem beyond the scope of what I've included? Thanks for any help
当我包含下面的代码时。调试时,visual studio 带我到“strcmp.asm”。请注意,我没有在我的任何代码中使用 strcmp()。代码有什么问题吗,或者这是否超出了我所包含的范围?谢谢你的帮助
const char* invarray[] = {"invader0.png", "invader1.png", "invader2.png", "invader3.png", "invader4.png"};
int i=0;
//Creates 55 invaders
for (int y=0; y<250; y+=50){
for (int x=0; x<550;x+=50){
Invader inv(invarray[y/50], x+50, y+550, 15, 15, 1, false, 250);
invaders[i] = inv;
}
}
Invader constructor:
入侵者构造函数:
Invader::Invader(const char *pic, int x, int y, int w, int h, bool dir, bool des, int point) : MovingObject(pic, x, y, w, h) , direction(dir), destroyed(des), b(0), points(point){};
MovingObject Constructor
移动对象构造函数
MovingObject::MovingObject(const char *pic, int x, int y, int w, int h):picture(pic), positionX(x), positionY(y), width(w), height(h) {};
采纳答案by Fraser
This line looks suspicious:
这一行看起来很可疑:
invaders[i] = inv;
You're never incrementing i
, so you keep assigning to invaders[0]
. If this is just an error you made when reducing your code to the example, check how you calculate i
in the real code; you could be exceeding the size of invaders
.
你永远不会增加i
,所以你一直分配给invaders[0]
。如果这只是您在将代码简化为示例时犯的错误,请检查您i
在实际代码中的计算方式;您可能会超出invaders
.
If as your comment suggests, you're creating 55 invaders
, then check that invaders
has been initialised correctly to handle this number.
如果正如您的评论所暗示的那样,您正在创建 55 invaders
,然后检查invaders
是否已正确初始化以处理此数字。
回答by Tneuktippa
"Access violation reading location 0x00000000" means that you're derefrencing a pointerthat hasn't been initialized and therefore has garbage values. Those garbage values could be anything, but usually it happens to be 0
and so you try to read from the memory address 0x0
, which the operating system detects and prevents you from doing.
“访问冲突读取位置 0x00000000”意味着您正在取消引用尚未初始化的指针,因此具有垃圾值。这些垃圾值可以是任何东西,但通常碰巧是这样0
,因此您尝试从内存地址中读取0x0
,操作系统检测到并阻止您这样做。
Check and make sure that the array invaders[]
is what you think it should be.
检查并确保数组invaders[]
是您认为应该是的。
Also, you don't seem to be updating i
ever - meaning that you keep placing the same Invader
object into location 0
of invaders[]
at every loop iteration.
此外,您似乎没有被更新i
过-这意味着你将保持在同一Invader
对象为定位0
的invaders[]
在每个循环迭代。
回答by Jay Medina
The problem here, as explained in other comments, is that the pointer is being dereference without being properly initialized. Operating systems like Linux keep the lowest addresses (eg first 32MB: 0x00_0000 -0x200_0000) out of the virtual address space of a process. This is done because dereferencing zeroed non-initialized pointers is a common mistake, like in this case. So when this type of mistake happens, instead of actually reading a random variable that happens to be at address 0x0 (but not the memory address the pointer would be intended for if initialized properly), the pointer would be reading from a memory address outside of the process's virtual address space. This causes a page fault, which results in a segmentation fault, and a signal is sent to the process to kill it. That's why you are getting the access violation error.
正如其他评论中所解释的,这里的问题是指针在没有正确初始化的情况下被取消引用。Linux 等操作系统将最低地址(例如前 32MB:0x00_0000 -0x200_0000)保留在进程的虚拟地址空间之外。这样做是因为取消引用归零的非初始化指针是一个常见的错误,就像在这种情况下一样。因此,当发生这种类型的错误时,不是实际读取恰好位于地址 0x0 处的随机变量(但不是指针在正确初始化时将用于的内存地址),而是从内存地址之外的指针读取进程的虚拟地址空间。这会导致页面错误,从而导致分段错误,并向进程发送信号以终止它。这就是您收到访问冲突错误的原因。