For an assignment for school, I need to create a program that takes one array and splices another into it, assigning the first X values of the first array into a new array, then all of the second, and then the rest of the first. It is also required that this is done by means of dynamically allocated arrays. I don't understand why, but for some reason, the heap is becoming corrupted and I can't figure out why. I am just now learning about pointers, so the solutions I have found don't make much sense to me.
If someone could point out exactly what I'm doing wrong, and explain it to me so that I can learn from my mistakes, I'd greatly appreciate it. Thanks!
#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] << ", ";
}
}
Combining C_Raj's answer, vinodsaluja's and Wander3r's comments:
you are allocating first and second arrays twice, once is enough, actually more is memory leak (vinodsaluja). Logically, since thirdarray is a combination of first and second arrays, its length should be sum of both array lengths, which is firstlength+secondlength not copylength. This is where heap corruption occurs (vinodsaluja). Finally ararys should be deallocated with delete[] (Wander3r).
C_Raj's code is what the result should be so I'm not copying it.