问题描述
在过去的 2 天里,我一直被一个似乎无法摆脱的违规行为所困扰.我使用了断点并找到了错误所在,但我只是希望你们中的一个人知道问题所在,而不必复制+粘贴我的所有代码 -.-
我要了
Escape.exe 中 0x1027cb1a (msvcr100d.dll) 处的第一次机会异常:0xC0000005:访问冲突写入位置 0xcccccccc.Escape.exe 中 0x1027cb1a (msvcr100d.dll) 处未处理的异常:0xC0000005:访问冲突写入位置 0xcccccccc.
现在,快速的谷歌搜索让我觉得发生了一些奇怪的事情.所有搜索结果都在谈论实际上并未指向任何地方的指针(0xcccccccc 是低内存地址?).
我还没有在我的代码中使用指针,但无论哪种方式,我都会粘贴函数并指出抛出异常的行(粗体):
void mMap::fillMap(){for(int i = 0; i
现在 myMap 是一个 Tile 类型的二维数组.几天前我有这个工作,直到我添加了一些其他类,它都停止工作了!
一个未初始化的指针,或者一个存储在内存中已被释放的指针.我认为 cccccccc
是第一个,cdcdcdcd
是第二个,但它因编译器/库实现而异.
对于您的特定代码,可能 myMap
尚未分配,那么 myMap[0][0]
将导致对 0xcccccccc 的访问尝试
.
也可能发生 myMap
是您的类的开头,并且类指针未初始化:
类mMap{平铺 myMap[10][20];民众:void f() { myMap[0][0] = 0;}};mMap*什么;什么-> f();//什么是无效指针
发生这种情况是因为成员函数不是虚拟的,所以编译器知道要运行什么代码并将对象指针作为隐藏参数传递.最终编译器会发出如下计算:
this + offsetof(Whatever::myMap) + z * sizeof(myMap[0]) + i * sizeof(myMap[0][0])
this
未初始化,是 0xcccccccc
.显然 offsetof
部分为零,并且 i
和 z
在第一次循环中都为零,所以你得到 0xcccccccc +0 + 0 + 0
作为内存地址.
要对此进行调试,请使用调用堆栈并找到调用 fillMap
的函数.然后检查用于成员访问的指针 (->
) 来自的那个函数.
For the past 2 days I've been stuck on a violation which I can't seem to get to go away.I've used break points and located where the error is, but I'm just hoping one of you will know what the issue is without me having to copy+paste all my code -.-
I'm getting
Now, a quick google search makes me think there's something peculiar going on. All the search results talk about pointers not actually pointing anywhere (0xccccccccc is a low memory address?).
I'm yet to use pointers in my code but either way I'll paste the function and point out the line the exception gets thrown (in bold):
void mMap::fillMap(){
for(int i = 0; i <= 9; i++){
for(int z = 0; z <= 19; z++){
Tile t1; // default Tile Type = "NULLTILE"
myMap[i][z] = t1;
}
}
}
Now myMap is a 2d array of type Tile. I had this working a couple of days ago until I added some other classes and it all stopped working!
Either an uninitialized pointer, or a pointer stored in memory that's been freed. I think cccccccc
is the first and cdcdcdcd
is the second, but it varies with compiler/library implementation.
For your particular code, probably myMap
hasn't been allocated yet, then myMap[0][0]
would result in an access attempt to 0xcccccccc
.
It can also happen that myMap
is the beginning of your class, and the class pointer was uninitialized:
class mMap
{
Tile myMap[10][20];
public:
void f() { myMap[0][0] = 0; }
};
mMap* what;
what->f(); // what is an invalid pointer
This happens because the member function is not virtual, so the compiler knows what code to run and passes the object pointer as a hidden parameter. Eventually the compiler emits a calculation like:
this + offsetof(Whatever::myMap) + z * sizeof(myMap[0]) + i * sizeof(myMap[0][0])
this
, being uninitialized, is 0xcccccccc
. Evidently the offsetof
part is zero, and i
and z
are both zero the first time through your loop, so you get 0xcccccccc + 0 + 0 + 0
as the memory address.
To debug this, use the call stack and find the function that called fillMap
. Then check in that function where the pointers used for member access (->
) came from.
这篇关于访问冲突写入位置 0xcccccccc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!