Dynamic memory allocation help

Hi everyone, having some problems with my homework assignment. Assignment is as follows:
Write a function that accepts an int array and the array's size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initial-ize the unused elements of the second array with 0. The function should return a pointer to the new array.

So far I have:
#include <iostream>
using namespace std;

int * doubleArray(int array[], int _size);

int* doubleArray(int array[], int _size)
{
int arraySize = _size*2;
int * _pointer;
_pointer = new int[arraySize];

for (int count = 0; count < arraySize; count++)
{
_pointer[count] = 0;
}

for (int count = 0; count < _size; count++)
{
_pointer[count] = array[count];
}

return _pointer;
}


int main (int argc, const char * argv[])
{
const int SIZE = 5;
int array[SIZE] = {2,4,6,8,10};
int *pointer = 0;

cout << "The array of size " << SIZE << " contains: \n";
for(int count = 0; count < SIZE; count++)
cout << array[count] << " ";

pointer = doubleArray(array, SIZE);

cout << "\n\nThe new array contains: \n";
for(int count = 0; count < SIZE*2; count++)
cout << array[count] << " ";

return 0;
}

My problem lies in the fact that every time i output the contents of the new array with the for loop back down in main, it outputs "2 4 6 8 10 0 0 5 1606416744 32767" I can't seem to figure out why the last three locations in the array are not being assigned 0. I tried putting the same for loop inside the doubleArray function and got the same output, so i know the problems not in the return of the function. Any ideas?
In main, after you double the array, you're printing the old array but using the doubled size. You're overstepping the bounds of the old array, causing you to print garbage. Doing this might have even caused your program to crash.

Also, remember to delete [] the pointer after you're done with it.
Topic archived. No new replies allowed.