-1

My goal in this program is to take two user entered arrays of a given size and merge them into one.

I'm able to successfully enter both arrays' size and their elements, but it fails to output the merged array- giving me this error:

"Error in `./a.out': free(): invalid pointer: 0x0..."

I've tried to debug but to no avail, I can't seem to figure out if I have incorrect syntax or I'm making an incorrect call.

Any help appreciated

#include<iostream>

using namespace std;
int* mergeArrays(int[], int[], int, int);
int arr1[0], arr2[0];
int  main()

{
    int size1, size2, i;

    cout<<"Enter the first array's size : ";
    cin>>size1;
    int *arr1 = new int[size1];

    cout<<"Enter the first array's elements : ";
    for(i=0; i<size1; i++)
    {
        cin>>arr1[i];
    }

    cout<<"Enter the second array's size : ";
    cin>>size2;

    cout<<"Enter the second array's elements : ";
    for(i=0; i<size2; i++)
    {
        cin>>arr2[i];
    }

    delete[] arr1;
    delete[] arr2;

    cout << mergeArrays;

}

int* mergeArrays(int arr1[], int arr2[], int size1, int size2){

    int i, k, size;
    int size3 = size1 + size2;
    int *mergeArr = new int[size3];

    for(i=0; i<size1; i++)
    {
        mergeArr[i]=arr1[i];
    }
    size=size1+size2;
    for(i=0, k=size1; k<size && i<size2; i++, k++)
    {
        mergeArr[i]=arr2[i];
    }
    cout<<"The merged array is: \n";
    for(i=0; i<size3; i++)
    {
        cout<<mergeArr[i]<<" ";
    }

    return mergeArr;
}
  • Remove `arr1` and `arr2` globals and declare and initialize `arr2` in the main like you did for `arr1` like this `int *arr2 = new int[size2];` Also, like Igor points out, you need to call the merge function. Right now you are just printing the address of the function. This is a good time to learn how to use a debugger and step through the program to understand it. – doug May 16 '19 at 04:00
  • I've done everything you said, and now I'm attempting to call the merge function. I'm new to working with pointers, would I need the `->` operator on this? I can't seem to call the function as I normally would. – Alex Zimmerman May 16 '19 at 04:12
  • Yeah, pointers can be quite confusing at first. Especially because they are pretty foreign to most languages. Don't worry, it will become clear after a bit. They are really fairly simple and in C++, unlike C, you won't need to use them too often. Especially the more complex uses. But you do need to know them. You might take a look at the resource books that the community recommends. https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282 – doug May 16 '19 at 04:54
  • To call the function use something like this: `int* retArray= mergeArrays(arr1, arr2, size1, size2);` `retArray` is a pointer to the merged array. You access its ints like `retArray[0]` where that's the first int. – doug May 16 '19 at 04:59

1 Answers1

0

Your program exhibits undefined behavior, by way of a buffer overrun. arr2 is an array of zero size, arr2[i] is accessing out of bounds for any value of i.

Also, you call delete[] arr2 but arr2 was not allocated with new

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85