简体   繁体   English

C++ 读取访问冲突错误

[英]C++ Read Access violation error

I am currently getting an "0xC0000005: Access violation reading location 0xcccccce0."我目前收到“0xC0000005:访问冲突读取位置 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.错误,我已经尝试诊断问题...我认为当我定义的 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];
}

When uninitialized, stack variables are filled with 0xCC bytes if compiled with msvc.未初始化时,如果使用 msvc 编译,堆栈变量将填充 0xCC 字节。 So 0xcccccce0 is probably the result of "this" being an uninitialized pointer variable on the stack plus _MySize offset in the object structure.所以 0xcccccce0 可能是“this”是堆栈上未初始化的指针变量加上 object 结构中的 _MySize 偏移量的结果。

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.您的代码存在许多明显的问题:例如,您的析构函数执行delete [] _datadelete [] myArray ,但_data从未在默认构造函数中初始化,并且myArray从未在复制构造函数中初始化。 And you're assignment operator can leave the object in an invalid state, if eg the new fails.你的赋值运算符可以将 object 保留在无效的 state 中,例如,如果new失败。 (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. (一般来说,如果您必须测试自赋值,几乎可以肯定您的赋值运算符已损坏。)这些问题中的任何一个都会导致未定义的行为,这可能会破坏自由空间领域,谁知道以后会导致什么问题。

Still, it would be interesting to see where size was called.尽管如此,看看调用size的位置还是很有趣的。 The error message suggests an invalid this ;错误消息表明this无效; that you've called the function with an invalid address.您使用无效地址拨打了 function。

在这篇小文章中,您可能会遇到其他类型的内存模式: http//www.docsultant.com/site2/articles/debug_codes.html

Breakpoints/watchlist.断点/监视列表。 I'm guessing that this is null.我猜是 null。

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.在您认为有 ArrayStorage 但实际上没有的地址处调用 size() 的某个地方。 Since it's 0xccccce0 I'd go with an unitialized pointer.因为它是 0xccccce0,所以我会使用一个初始化指针 go。

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?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM