Passing Variables Declared within Function as Return Variables

int * slice_arr(int * arr, int start, int end)
{
    int * slice_arr[LENGTH];
    ... // slicing goes here
    return slice_arr;
}

Looks right. But this won’t work.

The variables declared within a function call are allocated to the stack, and when the function ends (or returns), the memory within the stack will be garbage, because the value you allocated isn’t propagated anywhere on the stack anymore, therefore, when you try to catch the return value like this: int * sliced = slice_arr(arr, 1, 2); it doesn’t work.

malloc() is the only workable workaround to it, or using static, but that’s more dangerous.

#programming #memory