Function returning pointers

This is a discussion on Function returning pointers within the C Programming forums, part of the General Programming Boards category; Please see the code below :- insert Code: int main(){ int *p; int *fun(); p = fun(); printf("Value of i ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Function returning pointers

    Please see the code below :-

    insert
    Code:
    int main(){
        int *p;
        int *fun();
        p = fun();
        printf("Value of i %d" , *p);
        getch();
        return 0;
    }
    
    int *fun(void){
        int i = -214223;
        return (&i);
    }
    When i print the value of i in main , i expect it to give a garbage value since the variable i is local to fun.
    But actually its giving me the correct value.
    How is this possible....Please help

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Undefined behavior can mean anything. including giving you the result that you expect.
    you could try to say call any function before printing the value with printf().

  3. #3
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    The variable was local, but after the function finished the memory was given back to the system, but given there is so little going on in this program.. when the location was called again the value had been unchanged because no other function needed the space yet.

    That's my assumption.

    Try to call some other functions and do stuff before printing the value of i and see if you get garbage.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    yes when i actually call some other functions before printf() , it does give me a garbage value.

  5. #5
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    That would verify my thoughts.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    "Local to function" is a language concept. It should be respected for your own safety and program reliability.

    What the computer actually does with storage for such localized memory may be a superset of those concepts. If the locality of variables is no longer required - because the function has completed, then the system is free to reuse those portions of memory for the next requirement for "local" values. In current architectures it is most likely a common area set aside for such local uses. The stack.

    If there was no requirement as in your example where you did not call a second function, the computer had no reason to scramble that memory area. In other words you were looking at old data that could at any time be replaced. Hence documentation would say "undefined behavior".
    Last edited by nonoob; 12-16-2010 at 08:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 03:04 AM
  3. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 03:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21