ERROR :The thread 0x4c0c has exited with code 0 (0x0). Exception thrown: read access violation. **this** was nullptr

ERROR :The thread 0x4c0c has exited with code 0 (0x0). Exception thrown: read access violation. **this** was nullptr

我的一个程序 classes 在 c++ 中,我们需要设计一个 bool 数组 class- 包括长度和带有内存分配的 boolean 数组, 我收到错误消息 - 线程 0x4c0c 已退出,代码为 0 (0x0)。 抛出异常:读取访问冲突。 这是 nullptr.

当我尝试使用 changeLength 方法创建新数组时出现:

布尔数组 H 文件

#ifndef BOOL_ARRAY_H
#define BOOL_ARRAY_H
class boolArray
{
public:
    boolArray() { array = { NULL }; length = 0; }
    boolArray(int len);//setting new array according users input
    void intialize(int len);//setting new array according users input
    void changeLength(int len);//create new array with new length
    
private:
    bool validIndex(int index);//check if the number is in the relevant range
    bool* array;
    int length;
};
#endif


布尔数组 CPP 文件

boolArray::boolArray(int len)
{//ctor with parameter
    if (validIndex(len))//checking if the length is a valid number
        intialize(len);//set the new array
}
void boolArray::intialize(int len)
{//setting new array according users input
    length = len;//initialize length from user
    array = new bool[length];//memory allocation
    if (!array)//check if memory allocation succeeded
    {
        cout << "memory allocation failed!" << endl;//show message
        return;//go back
    }
    for (int i = 0; i < length; i++)
        array[i] = 0;//set all cells false
}
void boolArray::changeLength(int len)
{//create new array with new length
    if (length!=0)//if array exists //ERROR HERE
        delete[] array;//free memory
    intialize(len);//set new array
}
bool boolArray::validIndex(int index)
{//check if the number is in the relevant range
    if (index >= 0 && index <= length)
        return true;//if it is returns true
    else//if not in the range
        cout << "please enter a valid number!" << endl;//message
    return false;//return false
}

主要使用布尔数组CLASS---

boolArray* array;
int len;//new array length
        cout << "enter size of array " << endl;
        cin >> len;
        array->changeLength(len);

调试器: 谢谢!

array = new bool(length);//memory allocation

你的意思是new bool[length]

老实说,您还算幸运 array 为空。它可能是一直到奶奶家的任何东西,因为您忘记初始化它并将它指向 boolArray 的有效实例。

boolArray* array; // uninitialized pointer
int len;//new array length
cout << "enter size of array " << endl;
cin >> len;
array->changeLength(len); // Crom only knows what array points at.

最直接的解决方案是根本不使用指针

boolArray array; // no need for a pointer here
int len;//new array length
cout << "enter size of array " << endl;
cin >> len;
array.changeLength(len);

但我更喜欢

int len;//new array length
cout << "enter size of array " << endl;
cin >> len;
boolArray array(len); // no need to resize.

那么您需要修复 David Schwartz 强调的错误——它非常非常糟糕——而这个错误:

boolArray::boolArray(int len)
{//ctor with parameter
    if (validIndex(len)) //there is no length to validate against. 
                         // It hasn't been set yet.
        intialize(len); // length isn't set until in here
}

在构建数组之前检查数组访问的长度没有用,所以我只是将其删除。

boolArray::boolArray(int len)
{//ctor with parameter
    intialize(len);
}

还有一个警告:这个class没有遵守the Rule of Three并且不能被安全地复制。