1

Possible Duplicate:
Can a local variable's memory be accessed outside its scope?

I recently came across the following code:

#include <stdio.h>

int* abc () {
   int a[3] = {1,10,100};
   return a;
}
int* xyz () {
   int b[1] = {222};
   return b;
}
int main() {
   int *a, *b;
   a = abc();
   b = xyz();
   printf("%d\n", *a);
   return 0;
}

the output is 222. 'a' is pointing to the array declared inside the xyz().

my question is:

  1. why is a pointing to the array declared inside xyz().

  2. the array declared inside the function xyz() should go out of scope after the execution of the function. why is that not happening ?

Community
  • 1
  • 1
user1198065
  • 210
  • 3
  • 10

6 Answers6

8

2: It is happening, and the entire program has undefined behaviour. It is not a correct program, and there's little point musing about ifs and buts.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
3

You might see 222 because the memory that was used for the local array in abc has been used for something else - the stack for the function xyz. And you're passing around an address to that memory. Make a few more function calls and *a may contain some other value.

should go out of scope after the execution of the function. why is that not happening ?

The variable has gone out of scope. Using that address outside the function is incorrect code: using a pointer to local data returned from a function is undefined behavior.

pb2q
  • 58,613
  • 19
  • 146
  • 147
2

The variables a and b are automatic variables; using their adress in a other function is an undefined behavior. Anything can happen : you can't expect an output (eg, an optimizing compiler can delete some illegal code).

md5
  • 23,373
  • 3
  • 44
  • 93
1

to return a pointer it must be a pointer to dynamically allocated variable or static or global variable.

returning a pointer to a stack variable will cause you to have pointer to the stack which will be reused when you call a new method.

it happened in your case to reuse the stack variable for another array and overwrite the old value stored when you called the first method.

try to call printf again you will see different output because the first call to printf changed the stack content.

Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
  • why will another call to printf change the value on the stack? – user1198065 Sep 17 '12 at 17:34
  • because the parameters passed to the function are pushed into the stack and also the local variables are allocated from the stack. So be careful and follow my recommendations when returning pointers. – Mahmoud Fayez Sep 17 '12 at 18:31
0

why is that not happening ?

It does happen, just formally. Undefined behavior is not obligated to crash or to misbehave - they "anything might happen" means it can also run seemingly without any error. I just answered a similar question.

Community
  • 1
  • 1
0

The functions abc and xyz are each passing back an address to a locally created array. Subsequent calls are mashing the memory that was previously used (and passed back to you).

These are called automatic local variables.

You'll need to declare those arrays as static or allocate the memory in a different way.

Clinton Pierce
  • 12,859
  • 15
  • 62
  • 90