Thread: By reference vs value

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Ah, I love it when people advocate "do what you can get away with, and hope it won't break" rather than "apply a little more effort and do it right so you know it won't break".

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It applies to all types. Built-in types, when passed by value, have to be copied and that consumes memory.

    ...

    Last I checked, 8 was twice the value of 4. "Lightning fast" is not the same as "zero impact".
    Last I checked, 4 was as much as 4, and greater than 2 and 1. Consequently, there will not be any advantage of pass by reference over a copy of some built-in types with respect to memory, which, from what I see, is Daved's point when making the distinction between class types and built-in types.

    I note that even for passing a double the expense would also be "a fixed, and relatively small, upper bound", albeit possibly larger than that of pass by reference/pointer.
    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. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    That's what I believe anyway. I might be wrong
    Can I quote you on that?
    Oops too late, I just couldn't resist.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Last I checked, 4 was as much as 4, and greater than 2 and 1. Consequently, there will not be any advantage of pass by reference over a copy of some built-in types with respect to memory, which, from what I see, is Daved's point when making the distinction between class types and built-in types.

    I note that even for passing a double the expense would also be "a fixed, and relatively small, upper bound", albeit possibly larger than that of pass by reference/pointer.
    Also, the double being 8 bytes long, passed as a reference will be 4 bytes, but as soon as you come to ACCESS the double the compiler will have to read the reference to find the actual double, so depending on the usage pattern, although it saves some stack-space, it doesn't necessarily save any execution time.

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

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by grumpy View Post
    Ah, I love it when people advocate "do what you can get away with, and hope it won't break" rather than "apply a little more effort and do it right so you know it won't break".
    Ah, but I so love that, or at least I used to
    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