Thread: By reference vs value

  1. #1
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28

    By reference vs value

    Hi all,

    Im just working my way through my latest book, and the book states most c++ programmers when working in classes, pass objects via reference and not by value (pointers) because value can cause numerous error (lost pointers)? Is this true, or do most advanced programmer work with purely value?

    As i understand it, working with value (pointers), save vast amounts of memory, and therefore are a benifit over reference?

    Hope i got reference and value round the right way?

    Look forward to all the guru replies

    Cheers

    RocketMan

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Passing by value does not necessarily mean passing pointers. For example:
    Code:
    #include <iostream>
    #include <string>
    
    void print(std::string str)
    {
        std::cout << str << std::endl;
    }
    
    int main()
    {
        std::string str("Hello world!");
        print(str);
    }
    In the code above, str is passed by value.

    the book states most c++ programmers when working in classes, pass objects via reference and not by value (pointers) because value can cause numerous error (lost pointers)?
    I am not sure what the book is talking about, but passing by reference is typically used to avoid the copying that comes from passing the object by value, and/or to have changes to the object passed be reflected in the caller. Pointers can also be used for this purpose, but the reference syntax tends to have less clutter.

    What book is that, by the way?

    As i understand it, working with value (pointers), save vast amounts of memory, and therefore are a benifit over reference?
    That is just not true.

    Hope i got reference and value round the right way?
    I think you are confusing pass by value with the concept of pointers.

    I am not sure if this is too much for you to handle at the moment, but you could try reading an article on Pointers, References and Values.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have a misunderstanding. Passing by pointer is a form of "pass-by-reference" and is not pass by value.

    To pass-by-reference you can use references or pointers. To pass by value you use neither and a copy of the object is created.

    Pass-by-reference (with references or pointers) can save memory since it avoids a copy. This generally applies to classes rather than built-in types.

    C++ programmers often prefer passing by reference (or reference to const) over pointers because they have a more natural syntax and do not allow nulls to be passed. I'm not sure exactly what you mean by "lost pointers" but that might be what is being referred to.

  4. #4
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    Thanks for your reply.

    The book is: C++ in 21 days 4th edition, by Jesse Liberty. Have you read this book?

    Ok thanks, i will have a read of that link.

    Thanks

    RocketMan

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The book is: C++ in 21 days 4th edition, by Jesse Liberty. Have you read this book?
    No, but DougDbug recommended it in our book recommendation thread, so perhaps it is good with just a little awkward wording concerning this topic.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    Thanks, it might be my understanding. I will read that chapter again.

    Cheers.

    Do you ever sleep laserlight? You are on this forum alot - its a good think!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Daved View Post
    I'm not sure exactly what you mean by "lost pointers" but that might be what is being referred to.
    I think it may refer to that if you lose a pointer to an object allocated on the heap, it's a memory leak (lost?).
    Getting a pointer with NULL is typically used when the variable is optional or to say it isn't used, so there's not much that can go wrong here, I think.
    That's what I believe anyway. I might be wrong
    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 RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    Code:
    I think it may refer to that if you lose a pointer to an object allocated on the heap, it's a memory leak (lost?).
    Correct, i just explained it in slightly the wrong term - sorry.

    thanks again

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Bingo! Spot on!
    Anyway, this point is kind of moot in today's C++, since you can use smart pointers to get rid of the memory when no longer in use. Therefore, you never have to worry about deleting pointers.
    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.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Daved View Post
    Pass-by-reference (with references or pointers) can save memory since it avoids a copy. This generally applies to classes rather than built-in types.
    It applies to all types. Built-in types, when passed by value, have to be copied and that consumes memory.

    Passing any argument to a function logically involves copying something (eg passing a pointer means copying the value of the pointer; an analogous argument can be made for C++ references, except that the details of what is actually passed are up to the compiler). That copying implies some expense (eg machine instructions, execution time, memory consumption, use of registers).

    The advantage of passing by pointer or reference is that the expense has a fixed, and relatively small, upper bound. Whereas, when passing other types, that expense is influenced by the physical size of the object (sizeof(whatever)) which has no logical upper bound. The expense is also affected by any associated need for other operations (eg invoking a non-trivial copy constructor for class types) because of a need to create a temporary copy of whatever is being passed.

    The argument gets slightly blurrier with optimising compilers, as the standard specifically allows a compiler to avoid creating temporary copies in some circumstances.
    Last edited by grumpy; 01-13-2008 at 03:27 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As you know, even when passing through reference (or pointers), a dword is actually copied - the address of the argument, so it's moot to pass built-in types by reference unless it's an array.
    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.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    As you know, even when passing through reference (or pointers), a dword is actually copied - the address of the argument, so it's moot to pass built-in types by reference unless it's an array.
    Not true in general.

    Firstly, your comment about dword's is specific to some compilers/systems, it is not generally true.

    Second, I can offer a real counter-example. On quite a few 32 bit operating systems, a pointer is 32 bit (4 8-bit bytes) and a double is 8 bytes in size. Passing a double by value, on those systems, can therefore be more expensive than passing it by reference -- if we assume that copying four bytes is less expensive than copying eight.

    If the mechanism of passing arguments to a function becomes a critical concern affecting program performance or memory usage, then the only correct approach is to profile, analyse, and test the application behaviour for all target platforms.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I forget sometimes that there are non-32-bit operating systems out there. Anyway, on a 32-bit system, passing anything other double or INT64 as reference is pretty moot since it's typically converted to a 32-bit value and passed anyway.
    On 64-bit systems, a 64-bit value is passed.
    But 4 bytes vs. 8 bytes isn't a big difference. You probably won't notice any difference at all since it's lightning fast via exchange of one or two registers.
    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.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    I forget sometimes that there are non-32-bit operating systems out there. Anyway, on a 32-bit system, passing anything other double or INT64 as reference is pretty moot since it's typically converted to a 32-bit value and passed anyway.

    Quote Originally Posted by Elysia View Post
    But 4 bytes vs. 8 bytes isn't a big difference. You probably won't notice any difference at all since it's lightning fast via exchange of one or two registers.
    Last I checked, 8 was twice the value of 4. "Lightning fast" is not the same as "zero impact". If I perform that set of instructions a few billion times in a loop, I can be reasonably confident of detecting a performance difference because of that "twice" factor on many real world machines -- particularly those where two operations on registers cost more than one operation. And if I have an application that requires that loop .....

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Get your point.
    In time critical code, it's a big issue. Otherwise you can get away with it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM