# How to combine two arrays in a function with pointer

• (2 Pages)
• 1
• 2

## 20 Replies - 814 Views - Last Post: 02 December 2019 - 10:47 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417930&amp;s=61e20d84af16429ab61772bf66526811&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 PendeJoe

Reputation: 0
• Posts: 29
• Joined: 09-October 19

# How to combine two arrays in a function with pointer

Posted 28 November 2019 - 04:03 PM

Hey,
I have an old assignment which I was told to remake and combine two arrays in a function with help of pointer.

In the main function it looks like this:

cout << "\nCombine Two Arrays Into ONE...\n";
cout << "The content of the new array:\n";
array3 = combineArrays(array1, array2, size1, size2);
showArray(array3, size3);

my function looks like this:

int *combineArrays(int *array1, int *array2, int size1, int size2) {
int *array3 = nullptr;
int size3 = size1 + size2;

array3 = new int [size3];

for(int x = 0; x < size1; x++) {
array3[x] = array1[x];
}
for(int y = 0, k = size1; k < size3 && y < size2; y++, k++) {
array3[k] = array2[y];
}
return array3;
}

my output is correct when I show the combined array BUT then i just get alot of "0" I guess it's memory leak, how can I fix this issue?

Is This A Good Question/Topic? 0

## Replies To: How to combine two arrays in a function with pointer

### #2 Martyr2

• Programming Theoretician

Reputation: 5496
• Posts: 14,470
• Joined: 18-April 07

## Re: How to combine two arrays in a function with pointer

Posted 28 November 2019 - 05:20 PM

What does your showArray() function look like? Could be as simple as an loop that is not terminating correctly.

### #3 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• Joined: 05-May 12

## Re: How to combine two arrays in a function with pointer

Posted 28 November 2019 - 05:50 PM

Looks like the OP had two size3 variables. One local to combineArrays(), and another either in the main() or global scope. So setting size3 in combineArrays() does nothing to set the one in the other scope.

### #4 PendeJoe

Reputation: 0
• Posts: 29
• Joined: 09-October 19

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 02:13 PM

This is what my showArray(): looks like

void showArray(int* array, int size) {
int y = 9;

for (int x = 0; x < size; x++) {
cout << setw(4) << array[x];
if(x == y) {
cout << endl;
y += 10;
}
}
}

Quote

Looks like the OP had two size3 variables. One local to combineArrays(), and another either in the main() or global scope. So setting size3 in combineArrays() does nothing to set the one in the other scope.

is that why I get this output?

Please enter a positive integer for the size of the first array:
50
Please enter an integer value for the lower bound of the first array: 10
Please enter an integer value for the higher bound of the first array: 50
The 50 random integers between 10 and 50 are:
46 46 37 26 14 49 41 24 30 45
50 43 19 47 50 13 11 10 46 49
20 21 36 35 26 13 21 14 39 46
39 23 28 19 44 29 17 50 47 34
16 42 21 44 12 40 48 11 24 50

Please enter a positive integer for the size of the second array:
50
Please enter an integer value for the lower bound of the second array: 50
Please enter an integer value for the higher bound of the second array: 100
The 50 random integers between 50 and 100 are:
85 56 51 71 80 70 90 92 59 80
64 53 59 98 69 71 53 52 68 95
65 99 72 83 69 99 92 98 61 53
72 92 51 93 59 52 82 81 90 81
50 94 74 61 71 51 86 84 90 54

Combine Two Arrays Into ONE...
The content of the new array:
46 46 37 26 14 49 41 24 30 45
50 43 19 47 50 13 11 10 46 49
20 21 36 35 26 13 21 14 39 46
39 23 28 19 44 29 17 50 47 34
16 42 21 44 12 40 48 11 24 50
85 56 51 71 80 70 90 92 59 80
64 53 59 98 69 71 53 52 68 95
65 99 72 83 69 99 92 98 61 53
72 92 51 93 59 52 82 81 90 81
50 94 74 61 71 51 86 84 90 54
-566279930253291192774411932432 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0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 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 05912831925035628011927692119276921192760811927608
1197670412288
Process returned -1073741819 (0xC0000005) execution time : 8.462 s
Press any key to continue.

### #5 baavgai

• Dreaming Coder

Reputation: 7501
• Posts: 15,544
• Joined: 16-October 07

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 02:52 PM

Feels like you're working to hard for this one. The more variables, the more confusing. I'd go with something like:
int *combineArrays(int *array1, int *array2, int size1, int size2) {
// no int *array3 = nullptr;
int size3 = size1 + size2, i = 0;
int *array3 = new int [size3];

for(int j = 0; j < size1; j++) { array3[i++] = array1[j]; }
for(int j = 0; j < size2; j++) { array3[i++] = array2[j]; }
return array3;
}

Or, perhaps:
void copyArray(int *dst, int *src, int size);

int *combineArrays(int *array1, int *array2, int size1, int size2) {
int *array3 = new int [size1 + size2];
copyArray(array3, array1, size1);
copyArray(array3 + size1, array2, size2);
return array3;
}

Keeping in mind that there's something in the stl that will bascially do that copyArray bit.

Note, given your output, it really looks like you're not initializing size3?

Perhaps:
int *combineArrays(int &size3, int *array1, int *array2, int size1, int size2);
// or
void combineArrays(int &*array3, int &size3, int *array1, int *array2, int size1, int size2);

Hope this helps.

### #6 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• Joined: 05-May 12

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 03:41 PM

PendeJoe, on 29 November 2019 - 04:13 PM, said:

is that why I get this output?
Spoiler

Yes. For debugging purposes, add an extra line of code in your showArray() to print out the value of size.

### #7 PendeJoe

Reputation: 0
• Posts: 29
• Joined: 09-October 19

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 04:20 PM

So I´ve been looking through the code and the only thing I can think off causing this error is memory leaking, it might help if I show you guys the whole code :

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>

using namespace std;

int *fillArray(int, int, int);
void showArray(int*, int);
void sortArrayLow(int*, int);
void swapF(int*, int*);
void programmerInfo();
int *combineArrays(int *array1, int *array2, int size1, int size2);

int main() {
programmerInfo();
int low, high, size1, size2;
int size3 = size1 + size2;
int *array1;
int *array2;
int *array3;

cout << "***This Program will Combine Two Arrays Into One***\n";
do {
cout << "\nPlease enter a positive integer for the size of the first array: \n";
cin >> size1;
if(size1 <= 0) {
cout << "The size of the array MUST be greater or equal to 1! \n";
}
} while(size1 <= 0);

cout << "Please enter an integer value for the lower bound of the first array: ";
cin >> low;
cout << "Please enter an integer value for the higher bound of the first array: ";
cin >> high;

while(low >= high) {
cout << "The lower bound should be smaller than the higher bound!\n";
cout << "Please enter and integer value for the higher bound: ";
cin >> high;
}

cout << "The " << size1 << " random integers between " << low << " and " << high << " are:\n";

srand( (unsigned)time(0) );
array1 = fillArray(low, high, size1);
showArray(array1, size1);

do {
cout << "\nPlease enter a positive integer for the size of the second array: \n";
cin >> size2;
if(size2 <= 0) {
cout << "The size of the array MUST be greater or equal to 1! \n";
}
} while(size2 <= 0);

cout << "Please enter an integer value for the lower bound of the second array: ";
cin >> low;
cout << "Please enter an integer value for the higher bound of the second array: ";
cin >> high;

while(low >= high) {
cout << "The lower bound should be smaller than the higher bound!\n";
cout << "Please enter and integer value for the higher bound: ";
cin >> high;
}

cout << "The " << size2 << " random integers between " << low << " and " << high << " are:\n";

array2 = fillArray(low, high, size2);
showArray(array2, size2);

cout << "\nCombine Two Arrays Into ONE...\n";
cout << "The content of the new array:\n";

array3 = combineArrays(array1, array2, size1, size2);
showArray(array3, size3);

cout << "The content of the new array after sorting:\n";
sortArrayLow(array3, size3);
showArray(array3, size3);

cout << "Clear the dynamic array of the combined array...\n";

return 0;
}

int* fillArray(int low, int high, int size) {
int *arr = nullptr;
if(size <= 0) {
return 0;
}
arr = new int [size];

srand( time(0) );

for (int count = 0; count < size; count++) {
arr[count] = rand() % (high - low + 1) + low;
}
return arr;
}

void showArray(int* array, int size) {
int y = 9;

for (int x = 0; x < size; x++) {
cout << setw(4) << array[x];
if(x == y) {
cout << endl;
y += 10;
}
}
}

void sortArrayLow(int* numbers, int index) {
bool swapped;
for(int i = 0; i < index - 1; i++) {
swapped = false;
for(int j = 0; j < index - i - 1; j++) {
if(numbers[j] > numbers[j + 1]) {
swapF(&numbers[j], &numbers[j + 1]);
swapped = true;
}
}
if(swapped == false)
break;
}
}

void swapF(int* X, int* Y) {
int temp = *X;
*X = *Y;
*Y = temp;
}

int *combineArrays(int *array1, int *array2, int size1, int size2) {
int *combArr = nullptr;

combArr = new int [size1 + size2];

for(int x = 0; x < size1; x++) {
combArr[x] = array1[x];
}
for(int y = 0, k = size1; k < size1 + size2 && y < size2; y++, k++) {
combArr[k] = array2[y];
}

return combArr;
}

void programmerInfo() {
cout << left << setw(17) << "Programmer Name:" << setw(7) << right << "John Smith\n";
cout << left << setw(20) << "Programmer ID:" << setw(10) << right <<"1234567\n";
}

Would also like some feedback on current code if possible !

### #8 PendeJoe

Reputation: 0
• Posts: 29
• Joined: 09-October 19

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 05:03 PM

Okey, so I get the right output now but it still feels like something is wrong, any ideas?

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>

using namespace std;

int *fillArray(int, int, int);
int *combineArrays(int *array1, int *array2, int size1, int size2);

void showArray(int*, int);
void sortArrayLow(int*, int);
void swapF(int*, int*);
void programmerInfo();

int main() {
programmerInfo();
int low, high, size1, size2;

cout << "***This Program will Combine Two Arrays Into One***\n";
do {
cout << "\nPlease enter a positive integer for the size of the first array: \n";
cin >> size1;
if(size1 <= 0) {
cout << "The size of the array MUST be greater or equal to 1! \n";
}
} while(size1 <= 0);

cout << "Please enter an integer value for the lower bound of the first array: ";
cin >> low;
cout << "Please enter an integer value for the higher bound of the first array: ";
cin >> high;

while(low >= high) {
cout << "The lower bound should be smaller than the higher bound!\n";
cout << "Please enter and integer value for the higher bound: ";
cin >> high;
}

cout << "The " << size1 << " random integers between " << low << " and " << high << " are:\n";

int *array1 = new int [size1];

srand( (unsigned)time(0) );
array1 = fillArray(low, high, size1);
showArray(array1, size1);

do {
cout << "\nPlease enter a positive integer for the size of the second array: \n";
cin >> size2;
if(size2 <= 0) {
cout << "The size of the array MUST be greater or equal to 1! \n";
}
} while(size2 <= 0);

cout << "Please enter an integer value for the lower bound of the second array: ";
cin >> low;
cout << "Please enter an integer value for the higher bound of the second array: ";
cin >> high;

while(low >= high) {
cout << "The lower bound should be smaller than the higher bound!\n";
cout << "Please enter and integer value for the higher bound: ";
cin >> high;
}

cout << "The " << size2 << " random integers between " << low << " and " << high << " are:\n";

int *array2 = new int [size2];

array2 = fillArray(low, high, size2);
showArray(array2, size2);

cout << "\nCombine Two Arrays Into ONE...\n";
cout << "The content of the new array:\n";

int *array3 = new int [size1 + size2];

array3 = combineArrays(array1, array2, size1, size2);

delete [] array1;
delete [] array2;

showArray(array3, size1 + size2);

cout << "\nThe content of the new array after sorting:\n";

sortArrayLow(array3, size1 + size2);
showArray(array3, size1 + size2);

cout << "\nClear the dynamic array of the combined array...\n";

delete [] array3;

return 0;
}

int *fillArray(int low, int high, int size) {
int *arr = nullptr;

if(size <= 0) {
return 0;
}

arr = new int [size];
srand( time(0) );

for (int count = 0; count < size; count++) {
arr[count] = rand() % (high - low + 1) + low;
}
return arr;
}

void showArray(int* array, int size) {
int y = 9;

for (int x = 0; x < size; x++) {
cout << setw(4) << array[x];
if(x == y) {
cout << endl;
y += 10;
}
}
}

void sortArrayLow(int* numbers, int index) {
bool swapped;

for(int i = 0; i < index - 1; i++) {
swapped = false;
for(int j = 0; j < index - i - 1; j++) {
if(numbers[j] > numbers[j + 1]) {
swapF(&numbers[j], &numbers[j + 1]);
swapped = true;
}
}
if(swapped == false)
break;
}
}

void swapF(int* X, int* Y) {
int temp = *X;
*X = *Y;
*Y = temp;
}

int *combineArrays(int *array1, int *array2 , int size1, int size2) {
int *combArr = new int [size1 + size2];

for(int x = 0; x < size1; x++) {
combArr[x] = array1[x];
}
for(int y = 0, k = size1; k < size1 + size2 && y < size2; y++, k++) {
combArr[k] = array2[y];
}

return combArr;
}

void programmerInfo() {
cout << left << setw(17) << "Programmer Name:" << setw(7) << right << "John Smith\n";
cout << left << setw(20) << "Programmer ID:" << setw(10) << right <<"1234567\n";
}

### #9 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• Joined: 05-May 12

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 05:25 PM

Yes, the thing that you are missing is that you have a memory leak. You allocate a block of memory on line 77 but never free it.

### #10 PendeJoe

Reputation: 0
• Posts: 29
• Joined: 09-October 19

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 05:28 PM

If you look on line 93 you can see that I free it there, should I free it earlier?

### #11 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• Joined: 05-May 12

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 06:03 PM

Nope. On line 93, you free the memory allocated on line 149. This is because on line 79 you let array3 point to this block allocated on line 149 and forget about the block you allocated on line 77.

### #12 PendeJoe

Reputation: 0
• Posts: 29
• Joined: 09-October 19

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 06:50 PM

hmm.. okey, should I deallocate inside the main function or another function in that case?

### #13 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• Joined: 05-May 12

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 06:56 PM

Just declare the pointer on line 77 without initialization. Leave line 79 as is.

Or better yet, declare and initialize on line 77 with the return value of the call to combineArray(). Delete line 79.

### #14 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• Joined: 05-May 12

## Re: How to combine two arrays in a function with pointer

Posted 29 November 2019 - 08:31 PM

On a closer look of your code, it looks like you have similar leaks for array1 and array2 on lines 42 and 69.

### #15 PendeJoe

Reputation: 0
• Posts: 29
• Joined: 09-October 19

## Re: How to combine two arrays in a function with pointer

Posted 30 November 2019 - 12:28 AM

something like this:

int *array1 = fillArray(low, high, size1);
int *array2 = fillArray(low, high, size2);
int *array3 = combineArrays(array1, array2, size1, size2);

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }