-3

I've been trying to implement the concept of merging two dynamic sequences, which are already sorted when provided as input. Why is it producing 'Segmentation Fault' every time? The program first takes the size of two sequences and dynamically allocates memory for them. After the elements are taken as input, a function named merge() is called to implement the concept. It takes two pointers to the sequence and returns another sequence of twice the size of the entered sequences having merged sequences. Here, one should note that input sequences are already sorted.

#include <stdio.h>
#include <stdlib.h>

int* merge(int*, int*, int);

int main() {
    int *arr1, *arr2, *res;
    int size, i;
    printf("Enter the size of the array to be entered: ");
    scanf("%d", &size);
    arr1 = (int*)malloc(size * sizeof(int));
    arr2 = (int*)malloc(size * sizeof(int));
    if (arr1 == NULL || arr2 == NULL)
        exit(1);
    printf("Enter the elements for array 1 : \n");
    for (i = 0; i < size; i++)
        scanf("%d", arr1 + i);
    printf("Enter the elements for array 2 : \n");
    for (i = 0; i < size; i++)
        scanf("%d", arr2 + i);
    res = merge(arr1, arr2, size);
    if (res != NULL){
        for (i = 0; i < 2 * size; i++)
            printf("%d ", *(res + i));
    }
    else
        printf("The merging is not possible!");
    free(arr1);
    free(arr2);
    return 0;
}

int* merge(int* obj1, int* obj2, int size) {
    int* temp = (int *)malloc(2 * size * sizeof(int));
    int i, j, count;
    i = j = count = 0;
    if (temp == NULL)
        return NULL;
    while (count < 2 * size){
        while (*(obj1 + i) <= *(obj2 + j)){
            *(temp + count) = *(obj1 + i);
            count++;
            i++;
        }
        while (*(obj2 + j) <= *(obj1 + i)){
            *(temp + count) = *(obj2 + j);
            count++;
            j++;
        }
    }
    return temp;
}
Ron
  • 14,674
  • 4
  • 34
  • 47
CherryGot
  • 39
  • 8
  • [Please see this discussion on why not to cast the return value of malloc() and family in C..](https://stackoverflow.com/q/605845/2173917) – Sourav Ghosh Aug 22 '17 at 10:52
  • 2
    Have you used a debugger to find what is causing the seg fault? – Haris Aug 22 '17 at 10:52
  • Can we document the procedure of debugging segmentation fault using core file on Linux OS here on SO? People often come with questions like this. – Gaurav Pathak Aug 22 '17 at 10:55
  • Use std::vector. –  Aug 22 '17 at 10:55
  • 2
    in your merge function you never check the i and J bounds. Imagine first array having {1,2,3,4,5} and second array having {6,7,8,9,10} - what will your code do after copying all 5 values from arr1 to result? – Artemy Vysotsky Aug 22 '17 at 10:56
  • Every time you think "indexing is just dereferencing the sum of a base pointer and an offset, yay `*(temp + count)`!", you should write `temp[count]` instead. Shorter, clearer, and simpler. – unwind Aug 22 '17 at 11:19

1 Answers1

2
while( count < 2 * size ){
    while( *(obj1 + i) <= *(obj2 + j) ){
        *(temp + count) = *(obj1 + i);
        count++;
        i++;
    }

Consider all elements in obj1 are less than the first element of obj2

The inner while will access memory out of bounds because you are no where checking if i has reached end of obj1.You will do something like

*(obj1 + i) where i>=number of elements in obj1

You can use something like below.

while( i < size && j < size)
{
    if ( *(obj1 + i) <= *(obj2 + j) )
    {
        *(temp + count) = *(obj1 + i);
        count++;
        i++;
    }
    else
    {
        *(temp + count) = *(obj2 + j);
         j++;
         count++;
    }
}
while (i < size)
{
    *(temp + count) = *(obj1 + i);
    count++;
    i++;
}
while (j < size)
{
    *(temp + count) = *(obj2 + j);
    count++;
    j++;
}
Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34