Home > OS >  Adding two corresponding array elements and return the resultant array
Adding two corresponding array elements and return the resultant array

Time:12-06

Facing segmentation fault in the following code . Can anyone help me fix it?

#include<stdio.h>

int* multiply(int *arr1, int *arr2, int m);

int main(void){
    int m = 0;
    
    
    printf("Enter size of array1 and array2 >");
    scanf("%d",&m);

    int arr1[m],  arr2[m];
        printf("First array>");

    for(int i = 0; i < m;   i){
        scanf("%d", &arr1[i]);
    }
    printf("Second array> ");
    for(int j = 0; j < m; j  )
       scanf("%d", &arr2[j]);

    
    
    int* result = multiply(arr1, arr2, m);

    for(int i = 0; i < m;   i){
        printf("%d ", result[i]);
    }
    


}

int* multiply(int *arr1, int *arr2, int m){
    int res[m];
    printf("ok");
    for(int i = 0; i < m;   i){
        res[i] = arr1[i]   arr2[i];
    }
    printf("ok");
    return res;
}

Output should show like

Enter size of array1 and array2 >3

First array>5 1 7

Second array> 2 4 2

The resultant > 7 5 9

My output

Enter size of array1 and array2 >3

First array>5 1 7

Second array> 2 4 2

Segmentation fault

CodePudding user response:

The program has undefined behavior because the function multiply returns a pointer to the local array res that will not be alive after exiting the function. So after exiting the function the returned pointer will be invalid.

int* multiply(int *arr1, int *arr2, int m){
    int res[m];
    printf("ok");
    for(int i = 0; i < m;   i){
        res[i] = arr1[i]   arr2[i];
    }
    printf("ok");
    return res;
}

You need to allocate memory dynamically for the array. Also the parameters that denote arrays should have qualifier const because passed arrays are not changed within the function.

The function can be declared and defined the following way.

int * multiply( const int *arr1, const int *arr2, size_t n )
{
    int *res = NULL;

    if ( n != 0 && ( res = malloc( n * sizeof( int ) ) ) != NULL )
    {
        for( size_t i = 0; i < n;   i )
        {
            res[i] = arr1[i]   arr2[i];
        }
    }

    return res;
}

In main you should write

int* result = multiply(arr1, arr2, m);

if ( result != NULL )
{
    for(int i = 0; i < m;   i){
        printf("%d ", result[i]);
    }
}

free( result );

CodePudding user response:

The return varible of your function is local so it does not exist outside of the function scope, what I mean once you return a stack allocated variable from a function it scopes ends right then and there (unless that variable is defined as static), it is erased from the memory.

There are three fixes for it:

  1. make your return value variable static (this will preserve its value even when function's scope end);
  2. make your function void and instead pass the return value as a parameter to your function.
  3. use global variables for storing your result (highly discouraged though, but in your case it wont be a bad practice).

CodePudding user response:

In the multiply function, pass m as a constant integer, because the size of array number is always a constant.

int* multiply(int *arr1, int *arr2, const int m){
    int res[m];
    printf("ok");
    for(int i = 0; i < m;   i){
        res[i] = arr1[i]   arr2[i];
    }
    printf("ok");
    return res;
}

Do not forget to change the declaration of the function on line 3.

  • Related