Thread: is there a runtime error here?

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rnx40401 View Post
    I agree that getting abc back would be harder in a big system but in which case do we get a runtime error? when the memory is freed or when the memory is overwritten. My guess is that when the memory is overwritten we get some garbage back and no runtime error.
    You are correct. Memory is freed, though if you try to write there after that function, it might work, but don't count on it. Later it will probably be re-used for something else and you'll not get the result you were expecting from that variable.
    Returning a reference to a local variable is sort of "undefined behaviour." You don't know what's going to happen, so don't do it.

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by rnx40401 View Post
    I agree that getting abc back would be harder in a big system but in which case do we get a runtime error? when the memory is freed or when the memory is overwritten. My guess is that when the memory is overwritten we get some garbage back and no runtime error.
    As a c++ programmer you should just not do it end of story.

    For the curious here's why this rule was made into C++:
    Local variables are declared in a series of values in concurrent memory called the stack. When a function is called or a block is started space is delegated at the end of the stack for its local variables and the constructors are called. When a function returns or a block ends, the destructors of those variables are called. Then those variables are then deemed not part of the stack. The data is still there in most cases -- why bother clearing unused data.

    As such, any pointers or references to local variables are invalidated by the function return. However they will continue to work until another function is called or a local variable is declared*. When this happens the local variable(s) get declared at the end of the stack where the old local variables from the previous were. The old data is ignored completely.

    *Or possibly when the next block starts. I don't know what's convention when writing compilers. Some compilers may do it one way others another.

    This structure applies to just about all modern machines, big or small. Even embedded systems use it.
    Last edited by King Mir; 10-29-2007 at 01:57 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and the term "undefined behaviour" is just that - it is undefined. There are NO guarantees other than "it will most likely not always do exactly what you want". It may SOMETIMES (predictable if you understand what is going on in this particular compiler and environment). And changing the code ever so slightly can change the whole behaviour. Changing compiler flags, adding a function call, changing the usage of variables in your function, etc, etc.

    The exact behaviour when something "undefined behaviour" happens is also undefined. The two most likely scenarios are "nothing" happens and "a crash". Runtime error is possible, but unlikely. It could start printing the screen to your local printer, it could create a black hole and suck all the data in the system into it.

    Like King Mir says, the stack is where local variables are stored. They are generally not cleared when the called function is returning.

    Freed memory is no longer "in use", but some other function may at any time use it later on.

    Changing your code from
    Code:
    string &a = func();
    cout << a;
    to
    Code:
    string &a = func();
    func2();
    cout << a;
    could easily break a function depending on data stored on the unused stack.

    All of this gets even better if you use local variables after return in kernel mode, because every now and again, the processor will take an interrupt (keyboard, mouse, hard-disk, network, etc). The interrupt call will write data to the stack. So, say, 99 times out of 100, the returning of a local pointer is fine, but 1 time out of 100 it goes wrong - completely randomly!

    --
    Mats
    Last edited by Salem; 10-30-2007 at 01:08 AM. Reason: Fix tags
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    The best case scenario for undefined behavior is a crash, because then you know you have a problem.
    The worst case scenario is that it works as expected, because then you won't know you have a serious bug until one of your customers calls in to tech support.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  2. link with C runtime library
    By George2 in forum C++ Programming
    Replies: 26
    Last Post: 02-05-2008, 01:56 AM
  3. Visual Studio and .Net Runtime Framework dependency
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-08-2007, 07:52 AM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  5. Runtime error! Please help
    By Garfield in forum C Programming
    Replies: 7
    Last Post: 10-10-2001, 06:56 PM