passing local objects by reference

This is a discussion on passing local objects by reference within the C++ Programming forums, part of the General Programming Boards category; since local objects get destroyed once the function ends. so i am confused if it is good/bad to pass local ...

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    passing local objects by reference

    since local objects get destroyed once the function ends.
    so i am confused if it is good/bad to pass local objects by reference.

    for example:
    Code:
    void demoFunc()
    {
    	DemoClass demoObj;
    	passByReference(demoObj);
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    This example is fine because the function demoFunc won't end until the function passByReference returns. Thus it's safe.
    However, returning demoObj from the function demoFunc is bad because demoObj will be destroyed before the function that uses the object after demoFunc can use it.
    In this case, you either need to return it via a pointer (allocate on heap) or have the function returning a local object take a reference or pointer to a buffer to store the object.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    since local objects get destroyed once the function ends.
    so i am confused if it is good/bad to pass local objects by reference.
    It is perfectly fine to pass local objects by reference since they will still exist in the current scope when control enters the function called. It is bad to return references to local objects since they will not exist when control leaves the function in whose scope they exist.
    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

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks. but i was thinking what if passByReference() saves the object for a later use?
    i am really confused! sorry!

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    If it saves it for later use, then it's bad.
    It's really simple. The functions must never use the object for longer than the function that owns it. If any function tries to use the object after the function that owns it has returned, then the object will be destroyed.

    General rules:

    Good:
    The called function uses the object and then returns.

    Bad:
    The called function stores the object for later use.
    A function tries to use a local object returned by a function.

    These apply to references.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    thanks. but i was thinking what if passByReference() saves the object for a later use?
    i am really confused! sorry!
    Yes, if passByReference() makes a copy of the reference, then it will reference a dead object once the demoFunc() is finished, which is "UB".

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

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks again. but the real confusion is i don't have any idea if the function will save it for later use or not!
    and i am talking from my experience. i was writing Qt code today. and just stuck here. thinking about should i pass by reference or not

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    If unsure, it's best if you allocate the object on heap and pass via pointer/reference.
    It's better to be safe than to rely on undefined behavior.
    Otherwise, you can try to step through the code or ask experts if the function really will store the object for later or not (which is doubtful if the object is passed by reference).
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    thanks again. but the real confusion is i don't have any idea if the function will save it for later use or not!
    and i am talking from my experience. i was writing Qt code today. and just stuck here. thinking about should i pass by reference or not
    You should not need to know, in general, if the called function is taking the data by reference or not. It should be explicit that the object has a reference being held by the function, and such functions should be rare and "special" (such as setup functions, perhaps).

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

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    suppose the function that is passing the object is not a class member.
    in this case to make the object on heap has two options. one is make it global, other is make it local.
    now the go out of scope problem is solved. but the memory leak will occur if the object is made local. and other wise we have problems associated with globals.

    uuuurgh!! i think too much i think! sorry!

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    but the real confusion is i don't have any idea if the function will save it for later use or not!
    Check the documentation, but generally it is unlikely that the function will save it for later use, especially since its own lifetime is less than the lifetime of your calling function. Such a "saving" of a reference could happen if you were invoking a constructor of a class that has a reference member, because the lifetime of the object could exceed that of your calling function.

    and i am talking from my experience. i was writing Qt code today. and just stuck here. thinking about should i pass by reference or not
    You cannot make that decision: the Qt developers who designed the interface of that function decided whether you can pass by reference or not.

    EDIT:
    in this case to make the object on heap has two options. one is make it global, other is make it local.
    now the go out of scope problem is solved. but the memory leak will occur if the object is made local. and other wise we have problems associated with globals.
    Elysia was not suggesting global variables but new (with a smart pointer, for example), if you really wanted to be paranoid. Local objects exist on the stack, not the heap.
    Last edited by laserlight; 03-31-2008 at 07:17 AM.
    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

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    laserlight i actually meant this:
    Code:
    DemoClass * demoObj1;
    
    void demoFunc()
    {
    	demoObj1 = new DemoClass; // Option 1
    	DemoClass * demoObj2 = new DemoClass; // Option 2
    }

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But as stated, a function that "grabs" an object passed by reference should clearly state that it does so in the documentation of the function.

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

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Yes, you would need a global var or pass in a pointer that contains the object to pass, perhaps.
    But before you start making presumptions, make sure you know the function. Will it store a reference? Read the documentation. It should contain all knowledge you need.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks!
    this is the case here:

    Code:
    QtFunction(QtClass obj) { } // no reference
    MyFunction(QtClass & ref) { } // i thought i should use reference for efficiency
    i am 100% sure the QtFunction will have to save the obj somewhere because QtFunction is a constrcutor.
    MyFunction will invoke QtFunction, and that is where i was confused, if i declare it ref or non ref.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Passing Strings by Reference
    By xshapirox in forum C++ Programming
    Replies: 3
    Last Post: 10-11-2004, 09:35 AM

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