C++ 读取访问冲突错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10086102/
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
C++ Read Access violation error
提问by DorkMonstuh
I am currently getting an "0xC0000005: Access violation reading location 0xcccccce0." error and I have tried diagnose the problem...I think the problem comes in when my rule of 3 that I have defined comes in play and points me to here.
我目前收到“0xC0000005:访问冲突读取位置 0xcccccce0”。错误,我已经尝试诊断问题......我认为当我定义的 3 规则发挥作用并将我指向这里时,问题就出现了。
size_type size() const
{ // return length of sequence
return (this->_Mysize); <---------------------this line
}
I'm actually not sure if there is any problem at all, I have been dwelling on this for days on end.
我实际上不确定是否有任何问题,我已经连续几天都在思考这个问题。
Below is my rule of three
下面是我的三原则
ArrayStorage::ArrayStorage(){
myArray = new string[7079];
}
ArrayStorage::~ArrayStorage(){
delete[] _data;
delete[] myArray;
}
ArrayStorage::ArrayStorage(const ArrayStorage &A) {
_size = A.size();
_data = new string [size()];
for (int i = 0; i < size(); ++i)
_data[i] = A[i];
}
ArrayStorage& ArrayStorage::operator=(const ArrayStorage &A){
if (this != &A) {
delete [] _data;
_size = A.size();
_data = new string [A.size()];
for (int i = 0; i < size(); ++i) {
_data[i] = A[i];
}
}
return *this;
}
const string& ArrayStorage::operator[](int i) const{
assert((i >= 0) && (i < size()));
return _data[i];
}
string& ArrayStorage::operator[](int i){
assert((i >= 0) && (i < size()));
return _data[i];
}
回答by Nicolas Repiquet
When uninitialized, stack variables are filled with 0xCC bytes if compiled with msvc. So 0xcccccce0 is probably the result of "this" being an uninitialized pointer variable on the stack plus _MySize offset in the object structure.
未初始化时,如果使用 msvc 编译,堆栈变量将填充 0xCC 字节。所以 0xcccccce0 可能是“this”是堆栈上未初始化的指针变量加上对象结构中的 _MySize 偏移的结果。
回答by James Kanze
There are a number of obvious problems with your code: your destructor,
for example, does a delete [] _data
and a delete [] myArray
, but
_data
is never initialized in your default constructor, and myArray
is never initialized in the copy constructor. And you're assignment
operator can leave the object in an invalid state, if e.g. the new
fails. (In general, if you have to test for self assignment, it's
almost certain that your assignment operator is broken.) Either of
these problems result in undefined behavior which could corrupt the free
space arena, causing who knows what problem later.
您的代码有许多明显的问题:例如,您的析构函数执行 adelete [] _data
和 a delete [] myArray
,但
_data
从未在默认构造myArray
函数中初始化,也从未在复制构造函数中初始化。如果例如new
失败,您的赋值运算符可以使对象处于无效状态。(通常,如果您必须测试自赋值,则几乎可以肯定您的赋值运算符已损坏。)这些问题中的任何一个都会导致未定义的行为,这可能会破坏可用空间领域,导致谁知道以后会出现什么问题。
Still, it would be interesting to see where size
was called. The
error message suggests an invalid this
; that you've called the
function with an invalid address.
不过,看看在哪里size
被调用会很有趣。错误消息表明无效this
;您使用无效地址调用了该函数。
回答by James Kanze
In this small article there are other types of memory patters that you might encounter: http://www.docsultant.com/site2/articles/debug_codes.html
在这篇小文章中,您可能会遇到其他类型的内存模式:http: //www.docsultant.com/site2/articles/debug_codes.html
回答by dutt
I'd guess it's a pointer problem but it's impossible to tell without a stacktrace/backtrack. Somewhere you are calling size() at an adress where you think there's an ArrayStorage but there isn't. Since it's 0xccccce0 I'd go with an unitialized pointer.
我猜这是一个指针问题,但如果没有堆栈跟踪/回溯就无法判断。您在某个地址调用 size() 的某个地方您认为有一个 ArrayStorage 但没有。因为它是 0xccccce0 我会使用一个未初始化的指针。
For example, do you have something like this
例如,你有没有这样的事情
ArrayStorage storage1;
ArrayStorage* storage2;
*storage2 = storage1;
That would result in this or a similar error, invalid this pointer.
那会导致这个或类似的错误,使这个指针无效。
And why are you writing your own std::vector?
为什么要编写自己的 std::vector?
回答by CosminO
Breakpoints/watchlist. I'm guessing that thisis null.
断点/观察列表。我猜这是空的。