returning a pointer to a destroyed variable?

This is a discussion on returning a pointer to a destroyed variable? within the C++ Programming forums, part of the General Programming Boards category; i was wondering why this function doesn't lead to a complile error: int *funct() { int c=5; int *b; b=&c; ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    returning a pointer to a destroyed variable?

    i was wondering why this function doesn't lead to a complile error:

    int *funct()
    {
    int c=5;
    int *b;
    b=&c;

    return (b);
    }

    how can it return b if the variable c is destroyed when the function returns?

    furthermore, when it returns, the program can manipulate the value of c

    which shouldn't even exist...

    thanks for help

    p.s

    it won't compile if you try it with a reference...

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    I believe this is because the variable c stays in existence for a period of time before being overwritten or erased. Correct me if I'm wrong if I'm completely off.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    This will compile because compilers are not smart enough to follow the logic of your code, and see that what you are attempting to do will fail. Compilers find syntactical errors, but rarely find logic errors.

    In your example, the variable C will exist until it gets overwritten. See, the varable space gets popped off the stack once the function returns, but it will remain usable until you allocate more memory off the stack (and thus overwriting your variable).

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A pointer points to a place in memory. You can make a pointer (theoreticly, OS restrictions aside) point anywhere. You can even attempt to tweak whatever is there, or look at it. However, it may not like you screwing around with it, and you'll get things like "Segmentation fault" messages while your program goes down into a firey ball and trail of smoke.

    The actual variable instance is gone. However, that place in memory is still a place in memory. Physicly, if you have a block of ram, it actually exists, even if you don't do anything with it. If you make a pointer point to a chunk of it, it still physicly exists, even if your variable isn't using it any more.

    Think of a house on a street. Your buddy tells you his address and you go visit him at his house. Now, on a map you can locate that location. Say your buddy decides to move out. There is no one at the house any more, but it still exists. You can even take it further and say he burns his house down. The house may not exist any more, but the plot of land still does. It'll always be there, even if no one wants it any more.

    You can even go and try and camp out on the land. However, there's a chance the Cops will show up and Segmentation Fault your ass off to jail for vagrancy...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I can't improve on quzah's explanation. I will say that since you've chosen C/C++ as your language this is just the sort of thing you have to watch out for. C was designed from the perspective of the programmer being smart enough to have a lot of freedom. So it lets you do things that don't make sense. Now, if you know what you're doing that's great! You probably wont screw up. But there are lots of people who would try to use the pointer you describe. This makes C++ a language that is not "safe".

    On the other hand, a language that dumbs things down for a target audience that isn't smart enough to handle programming is too restrictive and very frustrating for a real programmer. (I program in Ada for a living. This is one of my pet peeves with Ada.)
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    C was designed from the perspective of the programmer being smart enough to have a lot of freedom.
    I think the motto was "trust the programmer", nobody said anything about them being smart.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    wow. you guys are a great help. thanks a bunch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-09-2008, 10:53 AM
  2. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 02:09 PM
  3. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 07:49 PM
  4. eh, help?
    By The_Nymph in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 03:27 PM
  5. returning a pointer
    By uler in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 02:59 PM

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