Passing dynamic array to a function

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

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
using namespace 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?

Thanks for any help.
Last edited on

I did an example of this here: http://www.cplusplus.com/forum/beginner/130325/

Hope this helps :)
closed account (j3Rz8vqX)
-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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
using namespace 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 ;

}
Last edited on
Topic archived. No new replies allowed.