-1

I am trying to create a merge function for two array structures in c++ but am coming up with a bad access error that I don't know how to solve. The error comes up when I am trying to swap the element in the smaller array into the larger, merged array. The code doesn't even go through a single iteration. All three of i, j, and k remain at 0. Any help would be greatly appreciated! Here is the code:

struct Array
{
    int *A;
    int size;
    int length;
};
void display(Array arr){
    for (int i = 0; i < arr.length; i++)
        std::cout << arr.A[i] << std::endl;
}
Array merge(Array arr1, Array arr2){
  Array arr3;
    arr3.length = arr1.length + arr2.length;
    arr3.size = arr1.length + arr2.length;
    int i = 0, j =0, k =0;
    while(i <arr1.length && j < arr2.length){
        if (arr1.A[i] < arr2.A[j])
        {
            arr3.A[k] = arr1.A[i];  //(The error is displayed here: Thread 1: EXC_BAD_ACCESS (code=1, address=0x28))
            k++;
            i++;
        }
        else if (arr2.A[j] < arr1.A[i])
        {
            arr3.A[k] = arr2.A[j];
            k++;
            j++;
        }
    }
    for (; i< arr1.length; i++)
    {
        arr3.A[k]=arr1.A[i];
        k++;
    }
    for (; i< arr2.length; j++)
       {
           arr3.A[k]=arr2.A[j];
           k++;
       }
    return arr3;
}

int main() {
    Array arr1;
    arr1.size = 10;
    arr1.length = 5;
    arr1.A = new int[arr1.size];
    arr1.A[0]= 2;
    arr1.A[1]= 6;
    arr1.A[2]= 10;
    arr1.A[3]= 15;
    arr1.A[4]= 25;
    Array arr2;
    arr2.size = 10;
    arr2.length = 5;
    arr2.A = new int[arr2.size];
    arr2.A[0]= 3;
    arr2.A[1]= 4;
    arr2.A[2]= 7;
    arr2.A[3]= 18;
    arr2.A[4]= 20;
    Array arr3 = merge(arr1, arr2);
    display(arr3);



    return 0;
}
273K
  • 29,503
  • 10
  • 41
  • 64
thekvector
  • 11
  • 4
  • 2
    It a good time to learn [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – 273K May 27 '20 at 01:36

2 Answers2

1

Your Array arr3 does not allocate any memory for its int *A field. It's natural that it would not work.

Anyway, your implementation of Array is very poor. Don't reimplement arrays unless you have a good reason; use std::vector instead.

If you really need to implement an Array on your own, then learn about encapsulation, make a class with a constructor, and allocate/delete your data (*A) field properly. Remember, using pointers and heap memory without understanding them is a recipe for disaster.

fvla
  • 121
  • 2
  • Thanks for the catch! And yeah usually I do use vectors, but the question in the book is wanting to me to create operations for arrays from scratch :( – thekvector May 27 '20 at 01:49
  • 1
    You should definitely stay away from using structs for that purpose. Learn to use classes and encapsulation through public/private fields. Then learn how to create/destroy array data using new/delete (std::shared_ptr isn't designed for this). Make sure you form your constructor and destructor for the object well to avoid memory leaks and access violations! – fvla May 27 '20 at 01:53
  • Will do, really appreciate the tips! – thekvector May 27 '20 at 01:55
0

Easy: arr3.A is not initialized. It's a pointer. What does it point to?

Suggestion: learn about dynamic memory allocation.

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32