a urget help from someone!!!!!!!

Discussion in 'C' started by vignesh1988i, Oct 10, 2009.

  1. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    int * fun();
    void main()
    {
    int *l;
    clrscr();
    l=fun();
    printf("hi : ");
    printf("%d",*l);
    getch();
    }
    int *fun()
    {
    int j=90;
    return &j;
    }

    pll.... say the output for this with a proper justifications !!!!!!!!!:smug::snore:
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What is your expectation of the output and did you try running it ?
     
  3. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    YA of course.... sir :) it's printing some garbage value... i know it will print garbage value... but I need to know the reason why it's printing so....

    And if i don't place that printf() statement , the value is correctly printing as 90...... so.. plz.. state me the reason .... sir... thank u:nice:
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You have to understand the memory mapping for that. I would try to explain. You are returning address of a variable which is out of scope which means you are returning memory address which can be allocated later and now when you call the printf with hi as parameter the returned memory is being filled by some value
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Yep, the output is undefined because j is local to fun() and its address is no longer valid when the function returns.

    So if the program needs to return the address of a new integer then you should create a new one on the heap with new or malloc, and remember somewhere in the program to delete/free it when you've finished with it, otherwise you get a memory leak.
     
  6. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    sir , how can it be invalid when the address returns to the main() function....... the main problem is the printf() statement , if i give that it's printing statement inbetween these it's printing some garbage value ,.... if i directly print as *j , the value is correcly printing....:disappoin:disappoin

    for shabbir sir .. sir kindly explain me in detail ,how the memory mapping is done >... :)
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    OK, the address itself isn't invalid, what is invalid is the assumption that j still exists. C and C++ pass arguments to functions by placing them on the stack, and local (automatic) variables are also created on the stack. So what your program does is to call fun() with no parameters; fun() creates j on the stack, then when it returns its stack frame and everything in it is discarded (although not necessarily changed). The main function then calls printf with a parameter "hi" which overwrites the memory that j previously occupied. Then you call printf a second time with two parameters which overwrite the memory that j previously occupied for the second time.

    So that is why the program displays garbage for j. It's because you are assuming that j still exists, and that assumption is wrong. The problem is NOT the printf statement - both of those are perfectly fine.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice