C++ 读取访问冲突错误

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

最佳答案

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

关于C++ 读取访问冲突错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10086102/

相关文章:

c++ - HLSL 在代码中获取线程组数和线程数

c++ - 在 wxwidgets 中,如何锁定在 gui 线程和工作线程之间共享的 vector ?

python - 在使用 imread 函数读取 jpg 文件时,是否有任何可能的原因在 opencv 中遇到一些困难?

javascript - 如何在 QWebView 中启用 localStorage?

c++ - sprintf 引起的 exc_bad_access

c++ - 向上转换和传递动态数组(C++)

c++ - CMake 库目标并不总是出现在 Visual Studio 2017 的启动项下拉列表中

c++ - 澄清指向函数转换的指针

c++ - 当我实现功能时 Sprite 变成白框

c++ - 使用 copyTo 函数断言失败 (C++)