我的特定代码的堆损坏问题

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

我正在编写 C++ 代码,因为我对动态分配和释放很陌生,所以堆损坏错误一直困扰着我。

#include<iostream>
#include<cstring>
#include<fstream>

using namespace std;
int getLength(char* string)
{
    int length = 0;
    while (string[length] != '\0')
    {
        length++;
    }
    return length;
}

void printData(char* string)
{
    for (int i = 0; string[i] != '\0'; i++)
    {
        cout << string[i];
    }
}

void StringConcatenate(char*& string1, char* string2)
{
    int length1 = getLength(string1);
    int length2 = getLength(string2);
    char* temp = new char[length1 + length2 + 2];
    int i = 0;
    int wordIndex = 0;
    while (i < length1)
    {
        temp[wordIndex] = string1[i];
        i++;
        wordIndex++;
    }
    temp[wordIndex] = ' ';
    ++wordIndex;
    i = 0;
    while (i < length2)
    {
        temp[wordIndex] = string2[i];
        wordIndex++;
        i++;
    }
    temp[wordIndex] = '\0';
    delete[] string1;
    string1 = temp;
    cout << "After Concatenation: ";
    printData(string1);
}

int getWords(char* string)
{
    int words = 0;
    for (int i = 0; string[i] != '\0'; i++)
    {
        if (string[i] == ' ')
        {
            words++;
        }
    }
    words++;
    return words;
}

int* getWordsLength(char* string, int words)
{
    int k = 0;
    int* array = new int[words];
    for (int i = 0; string[i] != '\0'; i++)
    {
        int j = i;
        while (string[j] != ' ' && string[j] != '\0')
        {
            j++;
        }
        array[k] = j - i;
        k++;
        i = j;
        if (string[i] == '\0')
        {
            break;
        }
    }
    return array;
}

char** StringTokens(char* string)
{
    int l = getLength(string);
    int words = getWords(string);
    int* wordsLength = getWordsLength(string, words);
    char** Tokens = new char* [words + 1];
    int k = 0;
    for (int i = 0; i < words; i++)
    {
        Tokens[i] = new char[wordsLength[i] + 1];
        int j = 0;
        while (string[k] != ' ' && string[k] != '\0')
        {
            Tokens[i][j] = string[k];
            j++;
            k++;
        }
        Tokens[i][j] = '\0';
        k++;
    }
    Tokens[words] = nullptr;

    delete[]wordsLength;
    wordsLength = nullptr;
    return Tokens;
}

char** InverseStringTokens(char* string)
{
    char** Tokens = StringTokens(string); // gets tokens
    int words = getWords(string); // words count
    int* wordsLength = getWordsLength(string, words); // words length
    char** inverseTokens = new char* [words + 1]; // +1 for the null char
    int a = 0, b = 0;
    for (int i = 0, j = words - 1; i < words; i++, j--)
    {
        inverseTokens[i] = new char[wordsLength[i] + 1];
        a = 0, b = 0;
        for (a, b; Tokens[j][b] != '\0'; a++, b++)
        {
            inverseTokens[i][a] = Tokens[j][b];
        }
        inverseTokens[i][a] = '\0';
    }
    inverseTokens[words] = NULL;

    int tokensSize = words + 1; // store size of Tokens
    for (int i = 0; i < tokensSize; i++)
    {
        delete[] Tokens[i];
    }
    delete[] Tokens;
    Tokens = nullptr;
    delete[] wordsLength;
    wordsLength = nullptr;
    return inverseTokens;
}


void ReverseSentence(char* string)
{
    char** reverseTokens = InverseStringTokens(string);
    char* reversedSentence = new char[getLength(string) + 2];
    int words = getWords(string);
    int* wordsLength = getWordsLength(string, words);
    int wordsIndex = 0;
    int temp = words - 1;
    for (int i = 0; i < words; i++)
    {
        for (int j = 0; j < wordsLength[temp]; j++)
        {
            reversedSentence[wordsIndex] = reverseTokens[i][j];
            ++wordsIndex;
        }
        temp--;
        if (i < words - 1)
        {
            reversedSentence[wordsIndex++] = ' ';
        }
    }
    reversedSentence[wordsIndex] = '\0';
    cout << "\nReversed string: " << reversedSentence << endl << endl << "Students List: \n";

    delete[]reverseTokens;
    delete[] reversedSentence;
    delete[] wordsLength;
    reverseTokens = nullptr;
    reversedSentence = nullptr;
    wordsLength = nullptr;
}

int main()
{
    int words;
    char* string1 = new char[30];
    char* string2 = new char[30];
    char** Tokens;
    char** inverseTokens;
    cout << "String 1: ";
    cin.getline(string1, 30);
    cout << "String 2: ";
    cin.getline(string2, 30);
    StringConcatenate(string1, string2);
    cout << endl;
    Tokens = StringTokens(string1);
    cout << "Tokens: ";
    for (int i = 0; Tokens[i] != nullptr; i++)
    {
        cout << endl << Tokens[i];
    }
    cout << endl;
    inverseTokens = InverseStringTokens(string1);
    cout << "Reverse Tokens: ";
    for (int i = 0; inverseTokens[i] != NULL; i++)
    {
        cout << endl << inverseTokens[i];
    }
    ReverseSentence(string1);

    for (int i = 0; inverseTokens[i] != NULL; i++)
    {
        delete[]inverseTokens[i];
    }
    for (int i = 0; Tokens[i] != NULL; i++)
    {
        delete[]Tokens[i];
    }
    delete[]inverseTokens;
    delete[]Tokens;
    delete[]string1;
    delete[]string2;
    inverseTokens = nullptr;
    Tokens = nullptr;
    string1 = nullptr;
    string2 = nullptr;
    return 0;
}

我调试了n次,仍然找不到源头或原因。我还下载了一些工具,如 Dr.Memory 等,但无法理解它们,所以最后前往堆栈溢出。请帮助和指导。

c++ heap-memory dynamic-memory-allocation heap heap-corruption
© www.soinside.com 2019 - 2024. All rights reserved.