C ++读取访问冲突错误

时间:2012-04-10 09:22:50

标签: c++

我目前正在收到“0xC0000005:访问冲突读取位置0xcccccce0”。错误,我已经尝试过诊断问题...我认为问题出现在我已经定义的3规则发挥作用并指向我这里。

size_type size() const
    {   // return length of sequence
    return (this->_Mysize); <---------------------this line
    }

我实际上不确定是否有任何问题,我已经连续几天都在研究这个问题了。

以下是我的三条规则

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];
}

5 个答案:

答案 0 :(得分:7)

如果未初始化,如果使用msvc编译,则堆栈变量将填充0xCC字节。所以0xcccccce0可能是“this”是堆栈上未初始化的指针变量加上对象结构中的_MySize偏移量的结果。

答案 1 :(得分:1)

您的代码存在许多明显的问题:您的析构函数, 例如,delete [] _datadelete [] myArray,但是 永远不会在您的默认构造函数中初始化_datamyArray 永远不会在复制构造函数中初始化。而你的任务 运算符可以使对象处于无效状态,例如, new 失败。 (一般来说,如果你必须测试自我分配,那就是 几乎可以肯定你的赋值操作符已经坏了。)其中一个 这些问题会导致未定义的行为,从而破坏自由行为 太空竞技场,导致谁知道以后会出现什么问题。

但是,看看size被调用的地方会很有趣。该 错误消息表明无效this;那你叫过 功能无效的地址。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

断点/监视列表。我猜这个这个是空的。

答案 4 :(得分:0)

我猜这是一个指针问题,但没有堆栈跟踪/回溯就无法判断。你在某个地方调用size()你认为有一个ArrayStorage但是没有。因为它是0xccccce0,所以我会使用一个单位指针。

例如,你有类似的东西

ArrayStorage storage1;
ArrayStorage* storage2;
*storage2 = storage1;

这会导致此错误或类似错误,此指针无效。

为什么要编写自己的std :: vector?

相关问题