Thread: warning: address of local variable ‘f’ returned

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    warning: address of local variable ‘f’ returned

    Code:
    #include<stdio.h>
    
    struct face
    {
      int c;                                                                                                                                                    
    };
    typedef struct face face;
    
    face * get_face()
    {
      face f = {4};
      return &f;
    }
    
    int main()
    {                                                                                                                        
      face * f = get_face();
      printf("face c: %d\n", f->c);
                                                                                                                                                                                                                                             
      return 0;
    }
    Here is the warning I get:

    warning: address of local variable ‘f’ returned


    Since I'm creating face on the stack, shouldn't it go away once the function exits? However, it only throws a warning and still runs perfectly fine (prints 4).

    Why does this run correctly? Is it guaranteed to run correctly?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    It runs correctly because you were lucky. It is not guaranteed to run correctly.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    The memory formerly known as f is still there, however it is no longer reserved by the stack and can be overwritten at any point in time. The common misunderstanding is that the memory gets filled with 0s or something when the variables are "removed" from the stack. All that is happening is the memory is no longer reserved.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It does "go away". The memory that was formerly occupied by it is now free to be overwritten by anything else that follows. Even how the printf call itself on the very next line down doesn't happen to overwrite that particular piece of memory before displaying the value is just pure dumb luck.

    It's just as though you were in a takeaway shop where they give out a number when you order, and then call out that number when they've made your meal. Holding onto that number for the following day doesn't mean that when they call that number out again that the food is for you. It might be, if you re-ordered and happened to be given the same number, but it quite likely is not.
    So don't go holding onto memory addresses of things that are no longer valid.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C code should really go into the C section, not the C++ section.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    C code should really go into the C section, not the C++ section.
    It is pretty hard for a beginner to distinguish C code from C++ code, yet when it compiles on both compilers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A beginner should know what language they are studying.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    @OP:
    Try this code
    Code:
    #include<stdio.h>
    
    int* get_local_var(int i)
    {
    	int ret = i;
    	return &ret;
    }
    
    int main()
    {
    	int* i1 = get_local_var(1);
    	int* i2 = get_local_var(2);
    	printf("i1: %d\n", *i1);
    
    	return 0;
    }
    i1 should print 1, right?

    @Others:
    Yes I know this code relies on undefined behaviour and it might not work as I intended. But I'm assuming that since the OP's code worked for him, this will to. I'm just trying to prove a point as to why you shouldn't keep pointers to local variables across function boundaries.

  9. #9
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by _Mike View Post
    @OP:
    Try this code
    Code:
    #include<stdio.h>
    
    int* get_local_var(int i)
    {
    	int ret = i;
    	return &ret;
    }
    
    int main()
    {
    	int* i1 = get_local_var(1);
    	int* i2 = get_local_var(2);
    	printf("i1: %d\n", *i1);
    
    	return 0;
    }
    i1 should print 1, right?
    ret is a local variable, you can't pass its address because it's gone when get_local goes out of scope. If you want to return local variable, return by value.

    Code:
    int get_local_var(int i)
    {
    	int ret = i;
         // do somethign else to ret here
    	return ret;
    }
    Unless you allocate the memory on the heap, then yes, you can return the pointer. But that is a pretty bad practice becuase you will forget to free it later.

    Code:
    int * get_local_var(int i)
    {
      int* ret=malloc(sizeof (int));
      *p=i;
      return p; <-------- have to free this in main() or whereever. When you have a hundred of these pointers, it becomes impossible to keep track of them. 
    }
    Last edited by nimitzhunter; 07-20-2011 at 10:44 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by nimitzhunter View Post
    ret is a local variable, you can't pass its address because it's gone when get_local goes out of scope. If you want to return local variable, return by value.
    Quote Originally Posted by _Mike
    @Others: Yes I know this code relies on undefined behaviour and it might not work as I intended. But I'm assuming that since the OP's code worked for him, this will to. I'm just trying to prove a point as to why you shouldn't keep pointers to local variables across function boundaries.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-11-2011, 10:50 AM
  2. Replies: 5
    Last Post: 08-14-2009, 03:15 AM
  3. Returning the Address of a Local Variable (Array)
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 08-20-2008, 02:03 AM
  4. Error - function returns address of local variable
    By tltying in forum C Programming
    Replies: 5
    Last Post: 05-28-2007, 02:26 AM
  5. Get local IP address
    By Snip in forum Networking/Device Communication
    Replies: 14
    Last Post: 02-28-2005, 03:09 PM