C++ 访问冲突写入位置 0xcccccccc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6085827/
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
Access violation writing location 0xcccccccc
提问by Linky
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 -.-
在过去的 2 天里,我一直被一个我似乎无法摆脱的违规行为所困扰。我已经使用了断点并找到了错误所在的位置,但我只是希望你们中的一个人知道问题所在,而不必复制+粘贴我的所有代码 -.-
I'm getting
我越来越
First-chance exception at 0x1027cb1a (msvcr100d.dll) in Escape.exe: 0xC0000005: Access violation writing location 0xcccccccc. Unhandled exception at 0x1027cb1a (msvcr100d.dll) in Escape.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Escape.exe 中 0x1027cb1a (msvcr100d.dll) 处的第一次机会异常:0xC0000005:访问冲突写入位置 0xcccccccc。Escape.exe 中 0x1027cb1a (msvcr100d.dll) 处未处理的异常:0xC0000005:访问冲突写入位置 0xcccccccc。
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?).
现在,快速的谷歌搜索让我觉得发生了一些奇怪的事情。所有搜索结果都在谈论实际上并未指向任何地方的指针(0xcccccccc 是低内存地址?)。
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!
现在 myMap 是一个 Tile 类型的二维数组。几天前我有这个工作,直到我添加了一些其他类,它都停止工作了!
回答by Ben Voigt
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.
未初始化的指针,或存储在已释放内存中的指针。我认为cccccccc
是第一个和cdcdcdcd
第二个,但它因编译器/库实现而异。
For your particular code, probably myMap
hasn't been allocated yet, then myMap[0][0]
would result in an access attempt to 0xcccccccc
.
对于您的特定代码,可能myMap
尚未分配,然后myMap[0][0]
会导致尝试访问0xcccccccc
.
It can also happen that myMap
is the beginning of your class, and the class pointer was uninitialized:
也可能发生myMap
在您的类的开头,并且类指针未初始化:
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.
this
,未初始化,是0xcccccccc
。显然,offsetof
部分是零,i
并且z
均为零的第一次通过你的循环,所以你得到0xcccccccc + 0 + 0 + 0
的内存地址。
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.
要对此进行调试,请使用调用堆栈并找到调用fillMap
. 然后检查用于成员访问 ( ->
)的指针来自的那个函数。
回答by Billy ONeal
On MSVC++ and in debug mode, the debugging memory allocator sets all returned memory to 0xcccccccc, as a way to find cases of undefined behavior. In all likelihood, you never initialized myMap
, or some of the pointers inside of myMap
. Check your initialization code for bugs.
在 MSVC++ 和调试模式下,调试内存分配器将所有返回的内存设置为 0xcccccccc,作为查找未定义行为情况的一种方式。很有可能,您从未初始化myMap
,或 . 中的某些指针myMap
。检查您的初始化代码是否有错误。
回答by sean e
For all of the answers and comments happening in this question, here are good references about memory fills in Visual C++:
对于此问题中发生的所有答案和评论,以下是有关 Visual C++ 中内存填充的良好参考:
回答by Konard
Had similar error when I tried to fill string value in table element of my own-class type with for-loop. I declared 1000 elements in that table, so I've put something like that:
当我尝试使用 for 循环在我自己的类类型的表元素中填充字符串值时出现了类似的错误。我在那个表中声明了 1000 个元素,所以我放了这样的东西:
for (int i = 0; i <= 1000; i++)
{
TAble[i].name = "Some Guy";
TAble[i].age = 4;
}
Unfortunately as with string it occurred that I'm perhaps insisting on fillinf element that doesn't exist witch is element number 1000 in table. I managed to solve this by changing loop header, deleting equal sign before 1000.
不幸的是,与字符串一样,我可能坚持不存在的 fillinf 元素是表中的元素编号 1000。我设法通过更改循环标题,删除 1000 之前的等号来解决此问题。
Try to see whether you're not trying to call something that doesnt exist.
试着看看你是否不想调用不存在的东西。