初始化字符串的动态数组(C++)

初始化字符串的动态数组(C++),c++,C++,我正在为一个动态字符串数组创建一个容器类。我知道使用std::vector会更容易/更好,但这不是重点。在构造函数中找到初始化数组的正确方法时遇到问题。按照下面的方式,编译器仍然警告我没有使用变量lineArray。程序编译时警告lineArray未使用,然后在运行时挂起 MyBag::MyBag() { nLines = 0; std::string lineArray = new std::string[0] (); } void MyBag::ResizeArray(int

我正在为一个动态字符串数组创建一个容器类。我知道使用std::vector会更容易/更好,但这不是重点。在构造函数中找到初始化数组的正确方法时遇到问题。按照下面的方式,编译器仍然警告我没有使用变量lineArray。程序编译时警告lineArray未使用,然后在运行时挂起

MyBag::MyBag()
{
    nLines = 0;
    std::string lineArray = new std::string[0] ();
}
void MyBag::ResizeArray(int newLength)
{
    std::string *newArray = new std::string[newLength];
    //create new array with new length
    for (int nIndex=0; nIndex < nLines; nIndex++)
    {
        newArray[nIndex] = lineArray[nIndex];
        //copy the old array into the new array
    }
    delete[] lineArray; //delete the old array
    lineArray = newArray; //point the old array to the new array
    nLines = newLength; //set new array size
}
void MyBag::add(std::string line)
{
    ResizeArray(nLines+1); //add one to the array size
    lineArray[nLines] = line; //add the new line to the now extended array
    nLines++;
}

向救援发出警告。幸好您有编译器警告,否则这将是一个需要更长时间才能解决的bug

std::string lineArray = new std::string[0] ();
^^^^^^^^^^^
正在构造函数中声明名为lineArray with的新变量。您没有使用类成员1。成员lineArray指针仍将指向某些未初始化的内存

应该是

lineArray = new std::string[0] ();

向救援发出警告。幸好您有编译器警告,否则这将是一个需要更长时间才能解决的bug

std::string lineArray = new std::string[0] ();
^^^^^^^^^^^
正在构造函数中声明名为lineArray with的新变量。您没有使用类成员1。成员lineArray指针仍将指向某些未初始化的内存

应该是

lineArray = new std::string[0] ();

您正在构造函数中使用名为lineArray的局部变量。您想使用您的数据成员,例如:

MyBag::MyBag()
{
    nLines = 0;
    lineArray = new std::string[0] ();
}

您正在构造函数中使用名为lineArray的局部变量。您想使用您的数据成员,例如:

MyBag::MyBag()
{
    nLines = 0;
    lineArray = new std::string[0] ();
}

除了编译器报告的明显错误(即初始化局部变量而不是分配给实例变量)之外,您还有一个更严重的问题:如果将小于nLines的值传递给ResizeArray,则代码将显示未定义的行为,即将数据写入已分配区域的末尾。您需要按如下方式更改代码:

void MyBag::ResizeArray(int newLength)
{
    // Add a trivial optimization:
    if (newLength == nLines) {
        // No need to resize - the desired size is already set
        return;
    }
    std::string *newArray = new std::string[newLength];
    //create new array with new length
    for (int nIndex=0; nIndex < nLines && nIndex < newLength ; nIndex++)
    {   //                             ^^^^^^^^^^^^^^^^^^^^^
        newArray[nIndex] = lineArray[nIndex];
        //copy the old array into the new array
    }
    delete[] lineArray; //delete the old array
    lineArray = newArray; //point the old array to the new array
    nLines = newLength; //set new array size
}

除了编译器报告的明显错误(即初始化局部变量而不是分配给实例变量)之外,您还有一个更严重的问题:如果将小于nLines的值传递给ResizeArray,则代码将显示未定义的行为,即将数据写入已分配区域的末尾。您需要按如下方式更改代码:

void MyBag::ResizeArray(int newLength)
{
    // Add a trivial optimization:
    if (newLength == nLines) {
        // No need to resize - the desired size is already set
        return;
    }
    std::string *newArray = new std::string[newLength];
    //create new array with new length
    for (int nIndex=0; nIndex < nLines && nIndex < newLength ; nIndex++)
    {   //                             ^^^^^^^^^^^^^^^^^^^^^
        newArray[nIndex] = lineArray[nIndex];
        //copy the old array into the new array
    }
    delete[] lineArray; //delete the old array
    lineArray = newArray; //point the old array to the new array
    nLines = newLength; //set new array size
}

除了shadowed member变量和ResizeArray to small array问题之外,add方法中还有一个bug,如6602所示。调用ResizeArray后,nLines已经更新为新值,因此实际上您正在写入错误的数组位置,然后再次错误地递增nLines。确保写入到正确的位置,无需增加

void MyBag::add(std::string line)
{
    int oldLength = nLines;
    ResizeArray(nLines+1); //add one to the array size
    lineArray[oldLength] = line; //add the new line to the now extended array
}

除了shadowed member变量和ResizeArray to small array问题之外,add方法中还有一个bug,如6602所示。调用ResizeArray后,nLines已经更新为新值,因此实际上您正在写入错误的数组位置,然后再次错误地递增nLines。确保写入到正确的位置,无需增加

void MyBag::add(std::string line)
{
    int oldLength = nLines;
    ResizeArray(nLines+1); //add one to the array size
    lineArray[oldLength] = line; //add the new line to the now extended array
}

注意:您的add函数有缺陷。调整大小后,行数已增加::Resize,无需再次执行nLines++操作。我不知道您的包是否只是一件学习的东西,但使用std::vectorBag;会更容易注意:您的add函数有缺陷。调整大小后,行数已增加::Resize,无需再次执行nLines++操作。我不知道您的包是否只是一件学习的东西,但使用std::vectorBag;那就容易多了