Hello,
I am trying to pass a dynamic array to a function which will:
- Copy the contents of the array to a temporary dynamic array
- Change the array passed in to one size bigger
- Copy the elements from the temp array back into the newly changed array
- Insert an item into the last spot of the array
#include <iostream>
usingnamespace std ;
void make_array ( int Old [] , int & old_size , int toInsert ) ;
void zero_array ( int arry [] , int arry_size ) ;
void print_array ( int arry [] , int arry_size ) ;
int main ( ) {
int * array1 = new (nothrow) int [0] ;
int array1_size = 0 ;
for ( int i = 0 ; i < 5 ; i ++ ) {
cout << "Size before insertion: " << array1_size << endl ;
cout << "Array before insertion:\n" ;
print_array ( array1 , array1_size ) ;
make_array ( array1 , array1_size , i ) ;
}
}
void make_array ( int Old [] , int & old_size , int toInsert ) {
int i = 0 ;
int * temp = new (nothrow) int [ old_size ] ;
zero_array ( temp , old_size ) ;
for ( i = 0 ; i < old_size ; i ++ ) {
temp [ i ] = Old [ i ] ;
}
Old = new (nothrow) int [ old_size + 1 ] ;
for ( i = 0 ; i < old_size ; i ++ ) {
Old [ i ] = temp [ i ] ;
}
Old [ i ] = toInsert ;
old_size ++ ;
delete [] temp ;
}
void zero_array ( int arry [] , int arry_size ) {
for ( int i = 0 ; i < arry_size ; i ++ ) {
arry [ i ] = 0 ;
}
}
void print_array ( int arry [] , int arry_size ) {
for ( int i = 0 ; i < arry_size ; i ++ ) {
cout << arry [ i ] << endl ;
}
cout << endl ;
}
The output seems like a memory address but is just a very large number, what have I done incorrectly to cause this?
-Created an array equal to that of old array.
-Zero out new array.
-Copy data values from old array to temp array.
-Assign old array a new array with a size of 1 greater than itself.
--!Memory leak old array not deleted!
Apply delete []Old on line 37?
A possibly solution: (I've changed make_array: header and prototype, because I felt the behaviors were different; it was that of pointers)
#include <iostream>
usingnamespace std ;
void make_array ( int *&Old , int & old_size , int toInsert ) ;
void zero_array ( int arry [] , int arry_size ) ;
void print_array ( int arry [] , int arry_size ) ;
int main ( ) {
int * array1 = new (nothrow) int [0] ;
int array1_size = 0 ;
for ( int i = 0 ; i < 5 ; i ++ ) {
cout << "Size before insertion: " << array1_size << endl ;
cout << "Array before insertion:\n" ;
print_array ( array1 , array1_size ) ;
make_array ( array1 , array1_size , i ) ;
}
}
void make_array ( int *&Old , int & old_size , int toInsert ) {
int i = 0 ;
int * temp = new (nothrow) int [ old_size ] ;
zero_array ( temp , old_size ) ;
for ( i = 0 ; i < old_size ; i ++ ) {
temp [ i ] = Old [ i ] ;
}
delete []Old;
Old = new (nothrow) int [ old_size + 1 ] ;
for ( i = 0 ; i < old_size ; i ++ ) {
Old [ i ] = temp [ i ] ;
}
Old [ i ] = toInsert ;
old_size ++ ;
delete [] temp ;
}
void zero_array ( int arry [] , int arry_size ) {
for ( int i = 0 ; i < arry_size ; i ++ ) {
arry [ i ] = 0 ;
}
}
void print_array ( int arry [] , int arry_size ) {
for ( int i = 0 ; i < arry_size ; i ++ ) {
cout << arry [ i ] << endl ;
}
cout << endl ;
}