-3
int* twoSum(int* nums, int numsSize, int target, int* returnSize){

    int i = 0, j = 0;

    for(i=0; i < numsSize; i++)
    {
        for(j = i+1; j < numsSize; j++)
        {
            if(nums[j] == target - nums[i])
            {
                int *index = (int *)malloc(sizeof(int) * 2);
                index[0] = i;
                index[1] = j;
                return index;
            }
        }
    }
    return NULL;
}

I was writing the code for the following

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].

I tried debugging the code, i and j values 0 and 1 as expected. But now I'am facing the issue while returning the index. It is showing as segmentation fault. Can anyone please correct the above code?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Run your program with `gdb` and then run `backtrace`. It will show you which line it is segfaulting on. – Gillespie Jan 28 '20 at 18:31
  • 1
    The `return index` instruction cannot possibly segfault. Your code looks right, there's no problem with it. You need to add a [mcve] to your question. Your problem is somewhere else. – Marco Bonelli Jan 28 '20 at 18:38
  • your code is OK .. you have problem somewhere else. [my solution](https://onlinegdb.com/S1qJXbA-L) – houssam Jan 28 '20 at 18:53
  • I´ve seen you use `NULL` as return value: https://stackoverflow.com/questions/59437282/can-i-use-null-as-substitution-for-the-value-of-0 – RobertS supports Monica Cellio Jan 28 '20 at 19:41
  • @houssam I just saw your solution. But I didn't get what is purpose of typecasting malloc to void. Can you please explain? – chaya kumar Jan 28 '20 at 19:44
  • @chayakumar : many compilers throw warning `Warning: incompatible implicit declaration of built-in function ‘malloc` if you use `malloc` without including the header file: `#include `. but the code works !!. You can use it without casting such as `int *index = malloc(sizeof(int) * 2);`. see : [Do I cast the result of malloc?](https://stackoverflow.com/q/605845/992406) – houssam Jan 28 '20 at 20:07

2 Answers2

1

Looks like codeforces or similar platform problems. Segmentation Fault is happening because you have not set returnSize passed by caller as same is required for freeing returned array. Thus solution would be to set returnSize to 2.

H S
  • 1,211
  • 6
  • 11
  • but `returnSize` still not being used inside the function. I'd consider to remove though. – Muzol Jan 28 '20 at 19:02
  • @Muzol I agree. However, this code will be judged by online judge which requires `returnSize` as a param. That's just how it's supposed to work. – H S Jan 28 '20 at 19:05
0
#include <stdio.h>
#include <stdlib.h>
int* twoSum(int* nums, int numsSize, int target, int* returnSize)
{
    int i = 0, j = 0;
    for(i=0; i < numsSize; i++)
    {
        for(j = i+1; j < numsSize; j++)
        {
            printf("i=%d j=%d \n",i,j);
            if(nums[j] == target - nums[i])
            {
                int *index = malloc(sizeof(int) * 2);
                index[0] = i;
                index[1] = j;
                return index;
            }
        }
    }
    return NULL;
}

int main()
{
    int nums[] = {2, 7, 11, 15}; // works
    //int * nums = {2, 7, 11, 15}; //compile OK (some compilers) but Segmentation fault .
    int target = 9;
    int * result = twoSum(nums,4,target,NULL);
    if(result)
        printf("result[0]=%d result[1]=%d" ,result[0] , result[1]);
    return 0;
}
houssam
  • 1,823
  • 15
  • 27