How to maintain and return local object reference?

This is a discussion on How to maintain and return local object reference? within the C++ Programming forums, part of the General Programming Boards category; Hi guys, i'd like to return a locally defined object from a function by referece, but i'm even not sure ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30

    How to maintain and return local object reference?

    Hi guys,
    i'd like to return a locally defined object from a function by referece, but i'm even not sure if this is possible. If it is, please help, otherwise, i'll return it by value, as happened in the example below.
    Thanks,
    Ben

    Code:
    class the_class{
    .
    .
    .
    };
    
    the_function(){
    the_class the_object();
    .
    .
    .
    return the_object;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Don't return references to local objects because they go out of scope and are destructed when the function exits.

    It may be legal if the object was static.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    It's not possible.
    Allocate on heap or return by value.
    I don't recommend doing static either. That's for special purposes and usually such variables are not meant to be returned.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    But what will you have a reference to, when the object has gone out of scope?

    Edit:
    Wow, people were all over this question like a cheap suit
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Ok,
    thanks, this is what i thought, also it would be nice if it was possible to maintain the memory for better performance.
    Ben

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Don't bother. Thanks to NRVO, the compiler does that for you.
    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

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    It's possible. Allocate memory on heap.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Ok, thanks, i'll do that

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Why messing with heap allocation? He can just adjust the lifetime of his object as needed:

    Code:
    void the_function(the_class the_object& obj)
    {
       obj.manipulate();
    }
    
    int main()
    {
       the_class the_object obj;
       the_function(obj);
    }
    This is fast (no return by value / no heap allocation) and clear.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Yes, you can. But the only way to return a local object is to allocate it on the heap.
    Whether this is feasible or a good idea is an entirely different matter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Yes, you can. But the only way to return a local object is to allocate it on the heap.
    Whether this is feasible or a good idea is an entirely different matter.
    Well, that applies if you want to have a reference to something from inside another function. The other possibility is to NOT MAKE IT A REFERENCE - just make a copy as you return the object. That is a perfectly valid solution, as long as the object is copyable (and safe to copy - but if it's not safe to copy, then it should be made uncopyable).

    --
    Mats
    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.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Note what I said earlier. NRVO stands for named return value optimization. It means that in this situation:
    Code:
    someclass foo();
    
    int main()
    {
      someclass sc = foo();
    }
    ... the compiler is allowed to completely omit the copying. Since the implementation of returning large object always means that the calling function allocates space for the temporary and passes a pointer - in other words, the return value is turned into a hidden reference parameter - NRVO says that the compiler may actually pass a reference to the object the temporary is then assigned to. For this optimization, it is even allowed to assume that the object's copy constructor has no side effects! (Even when it can't see the body.)

    So allocating on the heap is a horrible pessimization, and passing an out parameter by reference does exactly the same on the binary level, but with a less convenient interface.
    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

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Is this NRVO specified in the standard and/or do the most c++ compilers utilize it?

    if yes, one can say: returning by value does never impact performance compared to returning by reference/pointer independent from the size of the returned object?
    what would be new to me. and it would explain, why I was never able to see any performance boost while changing fuction signatures from return by value to call by non-const reference.

    Thanks

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It is explicitly specified in the standard as an option (because it changes observable behaviour when the copy constructor has side effects), but not as a requirement. Also, it can only be applied when the return value is assigned to a newly constructed object. (I think.)

    Pretty much every compiler in the latest generation supports NRVO. GCC supports it since 3.2 or something, VC++ for quite some time, as does Intel C++. Some more exotic ones also mostly support it.
    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

  15. #15
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30

    Thanks

    Hello Guys.
    Sorry for not answering for quite a while, due to some problems related to my internet connection, and great thanks for all answers,
    Ben

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 07:32 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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