is there a runtime error here?

This is a discussion on is there a runtime error here? within the C++ Programming forums, part of the General Programming Boards category; is there a runtime error here when you call the get method? Code: #include <string> using namespace std; class A ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    is there a runtime error here?

    is there a runtime error here when you call the get method?
    Code:
    #include <string>
    
    using namespace std;
    
    class A {
    public:
        string& get() {
            string abc = "abc";
            return abc;
        }
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,447
    Why not just test and see? Of course, even if you do not get a runtime error, returning a reference to a local variable is still wrong.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Why not just test and see? Of course, even if you do not get a runtime error, returning a reference to a local variable is still wrong.


    Are you sure that even for a arge system we get an error?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,447
    Are you sure that even for a arge system we get an error?
    Large or small, it does not matter. It is wrong since the caller would refer to an object that no longer exists.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Quote Originally Posted by laserlight View Post
    Large or small, it does not matter. It is wrong since the caller would refer to an object that no longer exists.
    The question here is that is getting a runtime error a garuntee? I agree that it is wrong to refer to a local variable when it gets out of scope but in a large system is there a one in a million chance of getting abc back

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Runtime error? That should return a compiler error or warning.
    Whatever you may say, returning a local variable is always wrong.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by Elysia View Post
    Runtime error? That should return a compiler error or warning.
    Whatever you may say, returning a local variable is always wrong.
    Returning a reference or pointer to a local variable is wrong. Returning the variable by value, of course, is not.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Quote Originally Posted by brewbuck View Post
    Returning the variable by value, of course, is not.
    But that's not the issue here, is it?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,447
    The question here is that is getting a runtime error a garuntee?
    What is your rationale for wanting to know if there is such a guarantee?

    That should return a compiler error or warning.
    To confirm that: I get a compiler warning on the MinGW port of g++ 3.4.2.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Elysia View Post
    But that's not the issue here, is it?
    No, but it is what you, intentionally or not, said.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Quote Originally Posted by CornedBee View Post
    No, but it is what you, intentionally or not, said.
    Is it? That was never my intention, though it is correct: returning by value is safe, returning by reference is not. This is in regards to a local variable or object.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by rnx40401 View Post
    The question here is that is getting a runtime error a garuntee? I agree that it is wrong to refer to a local variable when it gets out of scope but in a large system is there a one in a million chance of getting abc back
    I believe what you'll get in that case is 'undefined behavior'. You might be able to reference the memory of the string you returned and it might work fine if that memory hasn't been overwritten; but then again, it could quite easily blow up (if you're lucky).

    So the simple answer is: Don't use variables beyond their lifetime.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Quote Originally Posted by laserlight View Post
    What is your rationale for wanting to know if there is such a guarantee?

    As I asked earlier is there a remote chance of getting abc back in a large system? I guess it would behave differently on a laptop from a 1500 system environment

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Quote Originally Posted by rnx40401 View Post
    As I asked earlier is there a remote chance of getting abc back in a large system? I guess it would behave differently on a laptop from a 1500 system environment
    No! Don't count on it!
    Keep it by returning a pointer or by value.
    It's even harder in big systems since the (mow) freed memory will be overwritten.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Quote Originally Posted by Elysia View Post
    No! Don't count on it!
    Keep it by returning a pointer or by value.
    It's even harder in big systems since the (mow) freed memory will be overwritten.
    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.

Page 1 of 2 12 LastLast
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, 12: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

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