动态内存分配和堆损坏

时间:2019-05-06 03:36:11

标签: c++ arrays pointers heap dynamic-memory-allocation

对于学校作业,我需要创建一个程序,该程序接受一个数组并将另一个数组拼接到其中,将第一个数组的第一个X值分配给一个新数组,然后将所有第二个,然后分配其余的首先。还需要通过动态分配的数组来完成此操作。我不明白为什么,但是由于某种原因,堆变得损坏了,我不知道为什么。我现在正在学习有关指针的知识,所以我发现的解决方案对我来说没有多大意义。

如果有人可以准确指出我在做什么,并向我解释,以便我可以从错误中学习,我将不胜感激。谢谢!

#include <stdlib.h>
#include <iostream>
#include <time.h>

int* createArray(int);
int* splice(int[], int[], int, int, int);
void arrayPrint(int []);

using namespace std;

int main(void)
{
    int firstLength, secondLength, copyLength;

    cout << "Enter the length of the first array: ";
    cin >> firstLength;
    cout << "Enter the length of the second array: ";
    cin >> secondLength;
    cout << "Enter the length of the first array to be copied: ";
    cin >> copyLength;

    int* firstArray;
    int* secondArray;
    int* thirdArray;

    srand(100);

    firstArray = createArray(firstLength);
    secondArray = createArray(secondLength);

    firstArray = new int[firstLength];
    for (int i = 0; i < firstLength; i++)
        firstArray[i] = rand() % 100;
    secondArray = new int[secondLength];
    for (int i = 0; i < secondLength; i++)
        secondArray[i] = rand() % 100;

    thirdArray = splice(firstArray, secondArray, firstLength, secondLength, copyLength);

    cout << "First Array: " << endl;
    for (int i = 0; i < firstLength; i++)
    {
        cout << firstArray[i] << ", ";
    }
    arrayPrint(firstArray);
    cout << endl << "Second Array: " << endl;
    for (int i = 0; i < secondLength; i++)
    {
        cout << secondArray[i] << ", ";
    }
    arrayPrint(secondArray);
    cout << endl << "Spliced Array: " << endl;
    arrayPrint(thirdArray);

    delete firstArray;
    delete secondArray;
    delete thirdArray;
    system("pause");
    return 0;
}

int* createArray(int arrayLength)
{
    int* createdArray;
    createdArray = new int[arrayLength];
    for (int i = 0; i < arrayLength; i++)
        createdArray[i] = rand();
    return createdArray;
}

int* splice(int firstArray[], int secondArray[], int firstLength, int secondLength, int copyLength)
{
    int* splicedArray;
    splicedArray = new int[copyLength];
    for (int i = 0; i < copyLength; i++)
    {
        splicedArray[i] = firstArray[i];
    }
    for (int j = 0; j < secondLength; j++)
    {
        splicedArray[j + copyLength] = secondArray[j];
    }
    for (int k = 0; k < firstLength - copyLength; k++)
    {
        splicedArray[k + copyLength + secondLength] = firstArray[k + copyLength];
    }
    return splicedArray;
}

void arrayPrint(int toPrint[])
{
    for (int i = 0; i < sizeof(toPrint) / sizeof(*toPrint); i++)
    {
        if ((i % 10) == 9)
            cout << toPrint[i] << endl;
        else
            cout << toPrint[i] << ", ";
    }
}

1 个答案:

答案 0 :(得分:0)

结合C_Raj的回答,vinodsaluja和Wander3r的评论:

您要分配第一和第二个数组两次,一次就足够了,实际上更多的是内存泄漏(vinodsaluja)。 从逻辑上讲,由于thirdarray是第一数组和第二数组的组合,因此其长度应为两个数组长度的总和,即firstlength + secondlength而非copylength。这是发生堆损坏的地方(vinodsaluja)。 最后,应使用delete [](Wander3r)释放Araries。

结果应该是C_Raj的代码,所以我不复制它。