0

This is my code.

int* compareTriplets(int a_count, int* a, int b_count, int* b, int* result_count)
{   
    *result_count=2;
    int arr[2];
    for(int i=0;i<3;i++)
    {
        if(a[i]>b[i])
        {
            arr[0]=arr[0]+1;
        }
        if(b[i]>a[i])
        {
            arr[1]=arr[1]+1;
        }
    }
    return arr;
}

Here is the error

Declared in compareTriplets

int arr[2]

Address of stack memory associated with local variable 'arr' returned clang(-Wreturn-stack-address)

When I use static keyword and write static int arr[2], it gives me Correct answer and no error.

Is returning local variables not possible?

  • 1
    OK, you have made an observation. What is your question now? Do you know what `static` keyword does? Do you know about scope and lifetime of storage objects? Do you know about initializing static and non-static objects? What part of that observation is puzzling you? – Gerhardh Jan 02 '22 at 13:25
  • I would expect one more warning in your code regarding *using value of `arr[0]` without initialization* and also for index 1. – Gerhardh Jan 02 '22 at 13:26
  • @Gerhardh I have edited the question. But, I have initialized the array inside the function, then why using arr[0] should give error? – Kajal Singh Jan 02 '22 at 13:38
  • The problem with `static` is that too can be overwritten - by another call to the function. Allocate the array memory with `calloc` and return *that*. – Weather Vane Jan 02 '22 at 13:40
  • 1
    With `int arr[2];` you *haven't* initialised the array, so `arr[0]=arr[0]+1;` is *undefined behaviour*. – Weather Vane Jan 02 '22 at 13:41
  • Variables defined locally are not initialized automatically. Also you do not provide any initializer list for your array. Therefore no, you did not inititlized `arr`. – Gerhardh Jan 02 '22 at 13:46
  • Your final question is not correct. Your function returns `int*`, i.e. the address of `arr`. That is not exactly "returning a local variable". – Gerhardh Jan 02 '22 at 13:48

2 Answers2

0

int arr[2] creates the array on the stack which is only valid inside of the function. One could say the lifetime of arr ends with the return. So you are returning an address to invalid memory. The second problem is that arr is never initialized, so the contents of arr could be anything but will very likely not be what you want.

Using static mitigates (not solves!) the problem since arr now lies in the BSS segment which (in C) is automatically initialized to zero. So the function will return a pointer to a valid address, but at the second call of your function it will still hold the old values which never will be reset. So your second call to the function will also return a wrong answer.

The solution would either be to allocate the array in the function and return the address to the new array (the caller then has to take care for the free()!), or you could give the array for the results as a parameter into the function and only fill it there.

Sinic
  • 348
  • 3
  • 10
0

It is showing the error because arr is a local variable to compareTriplets() function and after returning you can't access that location as it will get used by some other process and to overcome this error static keyword makes that memory live in memory till the program ends and hence solves the conflict of memory.