Thread: question for functions that return pointers

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    5

    question for functions that return pointers

    Hello, I need to know why the following piece of code works... It is strange for me...

    Code:
    #include <stdio.h>
    int * fct (void)
    { int x = 2; return &x; }
    
    void main(void)
    {
      printf("%d\n", *fct());
    }
    x is a local variable that must be deleted from memory when fct exits...

    I cannot explain why the program works using visual studio 2010.

    Please elucidate me

    Thank you,

    Alex

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    First, get your main function right: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com. Return an integer (zero) at the end too.

    It works by sheer dumb luck*. You return an address that was on the stack, but when fct is done, that address is invalid. The data (2) is still there simply because nothing else has come along yet and corrupted that part of the stack.

    * It doesn't actually work, not by any reasonable definition. What that sort of behavior (accessing invalid memory) results in is undefined behavior: Question 11.33

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you were reading the rules of basketball and it said that you can't hold on to the ball and run with it, would you pick up the ball and try runing with it right then? And would you expect some magical force to suddenly stop you?

    If you try that in a real game, you will quickly into problems, just as if you try doing what you're doing in a real world program you will quickly run into problems.
    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"

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <stdio.h>
    
    int* foo1()
    {
    	int var1 = 21;
    	return &var1;
    }
    
    void foo2()
    {
    	int var2 = 42;
    }
    
    int main()
    {
    	int* temp = foo1();
    
    	printf("temp is: %d\n",*temp);  // Displays 21
    
    	foo2();
    
    	printf("temp is: %d\n",*temp);  // Displays 42
    
    	getchar();
    
    	return 0;
    }
    Just because the observed behavior seems to work and looks good/reasonable doesn't mean that you should be doing it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    5
    hehe, thank you for your posts.

    nice examples..

    I never said I am using this, but, by mistake I compiled this piece of code, and I was wondering why it works correctly.

    Alex,

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by alexsoad View Post
    hehe, thank you for your posts.

    nice examples..

    I never said I am using this, but, by mistake I compiled this piece of code, and I was wondering why it works correctly.

    Alex,
    It doesn't work correctly. A better compiler would stop the program with an error message. But that's quite hard to achieve. Most compilers don't protect memory which you've used and lost, and so it appears to be still there, for a short time. However it's liable to be overwritten at any point, not necessarily in the next function call (the compiler might be calling internal functions without you being aware of it), so it's not safe to use.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [VERY Beginner question] Strings + functions + ...pointers?!
    By arthurhess in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2009, 02:44 PM
  2. Passing Pointers to Functions question..
    By transgalactic2 in forum C Programming
    Replies: 7
    Last Post: 10-18-2008, 03:51 PM
  3. A question about pointers in functions
    By occams razor in forum C Programming
    Replies: 3
    Last Post: 05-15-2007, 12:16 AM
  4. Replies: 4
    Last Post: 03-11-2005, 05:45 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread