0

In the following program, I get the output 1 0 0 2130567168 11 2686668 7 2686916

whereas according to me the output must be 1 2 3 4 5 6 7 8

because the array elements are stored in contiguous memory locations and I am accessing those elements in a contiguous fashion.

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

int *fun3();

int main(){
    int j, k;
    int *q = fun3();
    for(j = 0; j < 8; j++){
        printf("%d\t", *(q+j));
    }
}

int *fun3(){
    int a[] = {1,2,3,4,5,6,7,8};
    return a;
}

Please suggest any problems in my code or my reasoning.Why am I getting this unusual behaviour?

Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
  • possible duplicate of [C Returning char\[\] Warning "returns address of local variable"](http://stackoverflow.com/questions/14872518/c-returning-char-warning-returns-address-of-local-variable) – P.P Jun 16 '13 at 14:26

3 Answers3

3

The array a has automatic storage duration, which ends when the function fun3() returns. This means it's not legal to access it after this point - doing so has undefined behaviour, which means anything can happen.

caf
  • 233,326
  • 40
  • 323
  • 462
1

The lifetime of int a[] is limited to fun3, so you can not return it and expect it to stay valid.

Bryan Olivier
  • 5,207
  • 2
  • 16
  • 18
  • If *scope* is the reason then @BLUEPIXY's answer would also exhibit the same behaviour; but it isn't. – P.P Jun 16 '13 at 14:38
  • @KingsIndian, @BryanOliver No, the `static` in @BLUEPIXY's answer actually means "store this on the heap", so it is persistent across calls to the function (in theory, I guess a compiler could allocate some heap storage and copy between heap and stack on each entry and exit to the function, but I suspect none actually do). – Vatine Jun 16 '13 at 15:20
  • 1
    @Vatine They were referring to `scope` versus `lifetime`. And `static` doesn't mean "store this on the heap". "scope", "lifetime" and "static" have very specific meanings in this context. – Andrei Jun 16 '13 at 15:26
  • @Andrei Yes, but unless the compiler takes extra steps to ensure that the pointer to the static member is, indeed, copied between heap and stack, a pointer to a lexically-scoped static variable function will be sensibly accessible from outside the function. And access to one that isn't won't be. – Vatine Jun 16 '13 at 16:26
  • @Vatine actually such a `static` variable will in general just become global storage. It's name is just limited to the scope of `fun3`. – Bryan Olivier Jun 16 '13 at 16:29
  • @BryanOlivier Yes, but we were (I believe) discussing if a pointer to the variable would sensibly point to storage accessible from outside the function. – Vatine Jun 16 '13 at 17:51
  • @Vatine Ah, I understand. I sure hope it is, because otherwise a lot of code will break. But I'm going to look it up. – Bryan Olivier Jun 16 '13 at 17:56
  • 2
    @Vatine Phew, it seems it is. See 6.2.4 Storage durations of objects ad 3 "... has _static storage duration_. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup". Ad 2 defines the pointer to such storage. – Bryan Olivier Jun 16 '13 at 18:13
0

int a[] of function fun3 is local scope. scope of a is no longer valid if you get out of this function. It is one way it is because since an auto variable is storage class for example is to change the static.

int *fun3(){
    static int a[] = {1,2,3,4,5,6,7,8};
    return a;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70