C ++动态内存分配,在第二次第二次崩溃前使用删除

问题描述 投票:0回答:1

我正在构建一个程序来创建动态数:char **。C ++中的删除功能对我来说是新功能,所以也许我正在做一些基本错误,但我在互联网上找不到答案。

该代码在第一次工作并成功删除了数组,但是在第二次我得到了:“堆损坏”错误。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

int addString(char*** mArr, char*** bArr, int& stringIndex);
int copyStrings(char*** mArr, char*** bArr, int stringIndex);
void deleteArr(char*** arr, int stringIndex);

    using namespace std;
    int main()
    {
        int stringChoice = 0/*USER CHOICE*/, stringIndex = 0;//HOW MANY STRINGS ARE CURRENTLY!
        int searchIndex = 0;//HELP THE SEARCH FUNCTIONS 6 AND 7.
        char** mArr = NULL;//MAIN ARRAY
        char** bArr = NULL;//BETA ARRAY TO AVOID LOSE OF DATA.

        cout << "Welecome to string menu, what do you want to do?" << endl;
        while (stringChoice != 8)
        {
            cout << "\n1.Add a string\n"
                << endl;
            cin >> stringChoice;
            switch (stringChoice)
            {
            case 1:
                addString(&mArr, &bArr, stringIndex);
                for (int j = 0; j < stringIndex; j++)
                    cout << mArr[j] << endl;
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            case 8:
                break;
            }
        }
    }

        int addString(char*** mArr, char*** bArr, int& stringIndex)
        {
            if (mArr != NULL)
            {
                copyStrings(mArr, bArr, stringIndex);//COPYING mArr TO bArr.
                deleteArr(mArr, stringIndex);//DELETE EVERYTHING FROM mArr. AVOIDING MEMORY LEAK.
            }
            stringIndex++;//ADDING A STRING TO THE ARRAY.
            *mArr = new char* [stringIndex];//ALLOCATING THE NEW SIZE AND CHECKING ALLOCATION.
            if (*mArr == NULL)
                return 0;
            if (*bArr != NULL)//IF SOMETHING WAS COPIED FROM mArr TO bArr BEFORE.
            {
                for (int i = 0; i < stringIndex - 1; ++i)//COPYING bArr TO THE MAIN ARRAY!
                {
                    *mArr[i] = new char(strlen(*bArr[i]) + 1);
                    if (*mArr[i] == NULL)
                        return 0;
                    else strcpy(*mArr[i], *bArr[i]);
                }
                deleteArr(bArr, stringIndex - 1);//DELETE EVERYTHING FROM bArr.
            }
            char checkLength[80];//BUUFER TO CHECK THE USER INPUT LENGTH.
            cout << "Please enter your string!" << endl;
            cin.ignore(1);//IGNORE BUFFER CHAR THAT WILL BE EQUAL TO - "ENTER".
            cin.getline(checkLength, 80);
            *mArr[stringIndex - 1] = new char[strlen(checkLength) + 1];//ALLOCATING THE LENGTH OF USER INPUT.
            if (*mArr[stringIndex - 1] == NULL)//                        ADDING 1 FOR THE NULL TERMINATOR.
                return 0;//CHECKING ALLOCATION.
            strcpy(*mArr[stringIndex - 1], checkLength);
            return 1;
        }

        int copyStrings(char*** mArr, char*** bArr, int stringIndex)
        {
            *bArr = new char* [stringIndex];//STRING INDEX IS HOW MUCH STRINGS WAS ADDED
            if (*bArr == NULL)
                return 0;
            for (int i = 0; i < stringIndex; ++i)
            {
                *bArr[i] = new char(strlen(*mArr[i]) + 1);
                if (*bArr[i] == NULL)
                    return 0;
                strcpy(*bArr[i], *mArr[i]);
            }
            return 1;
        }

        void deleteArr(char*** arr, int stringIndex)
        {
            cout << "string index value: " << stringIndex << endl;
            //  for (int i = 0; i < stringIndex; i++)
            //  {
            //      cout << i << endl;
            //      cout << *arr[i] << endl;
            //  }
            for (int i = 0; i < stringIndex; i++)
            {
                delete[] * arr[i];
                cout << "DELETE " << stringIndex << endl;
            }
            delete[] * arr;
        }

我正在尝试再次使用NEW之前删除阵列,以避免内存泄漏。第一次像我说的那样工作,但是第二次.....我有很多印刷参考资料,所以请忽略,这只是我第一个C ++项目的一小部分。谢谢!

c++ memory new-operator dynamic-memory-allocation delete-operator
1个回答
1
投票

这是一个错误

*bArr[i] = new char(strlen(*mArr[i]) + 1);

应该是

*bArr[i] = new char[strlen(*mArr[i]) + 1];

您两次addStringcopyString犯此错误。

这里是另一个错误

            delete[] * arr[i];

应该是

            delete[] (*arr)[i];

但是实际上该代码可以用少一级的指针编写

    void deleteArr(char** arr, int stringIndex)
    {
        cout << "string index value: " << stringIndex << endl;
        for (int i = 0; i < stringIndex; i++)
        {
            delete[] arr[i];
            cout << "DELETE " << stringIndex << endl;
        }
        delete[] arr;
    }

但是有种种原因使这种代码不被接受。除非被告知您必须立即放弃使用这些技术编写此代码。 std::stringstd::vector是用于此任务的内容。

© www.soinside.com 2019 - 2024. All rights reserved.