Passing a paramater by reference

This is a discussion on Passing a paramater by reference within the C++ Programming forums, part of the General Programming Boards category; Okay, my questions are based on these threads: http://cboard.cprogramming.com/showthread.php?t=86243 http://cboard.cprogramming.com/showthread.php?t=86230 I was wondering, is it better to pass parameters by ...

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Passing a paramater by reference

    Okay, my questions are based on these threads:
    Array or Vector as augment

    simple question im not sure about

    I was wondering, is it better to pass parameters by reference (and const reference if we don't want to change the parameter) in all functions? I think it is better than copying the parameter to stack.

    If it isn't, why? What would the proper situation to pass parameters by reference (beside to have the ability to change the said parameters)?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I think if you are passing very large objects to the function and you have to use that function quite often, you'd want to stop copying the objects over and over by using either pointers or references. I don't know if the memory / speed difference would be significant though.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    A reference will ensure that no object is created when used as a parameter. This may have high implications if the object occupies a lot of memory, or it is slow to create.

    Assume a C++ string composed of "Hello, this is me here. I'm a string made of a lot of characters and I do occupy a lot of space, although I'm somewhat quick to build". This string has 133 characters. Each character occupies 8 bits on most systems. 8 x 133 = 1,064 bits. Now.. look at the following:

    Code:
    std::string concatenate(std::string str, std::string extra) {
      return str + extra;
    }
    
    int main() {
        std::string foo = "Hello, this is me here. I'm a string made of \
                                    a lot of characters and I do occupy a lot of space, \
                                    although I'm somewhat quick to build.";
    
       std::string bar = " I shouldn't really be passed by value";
    
       std::cout << concatenate(foo, bar);
    
    }
    Lots of wasted memory. First in main I allocate 1,064 bits from the stack with foo plus 38 x 8 = 304 bits with bar.

    Next I call concatenate(). Since I'm not passing by reference, two new string instances will be created and their copy constructors ran to initialize them. That's 1,064 + 304 bits on top of the previous ones. Inside the function I create a new string that is another 1,064 + 304 bits with that return statement. It's a temporary unnamed variable that is assigned the contents of both strings.

    After the return statement, the destructors for std::string str and std::string extra are ran. And back in main, after the cout statement is evaluated, the destructor for the temporary unnamed std::string that is the return type of concatenate() is also ran.

    If instead I pass both string as references, all the construction and destruction, plus extra memory use of str and extra are not performed.

    Now... think of a more complex objects like an user-defined object occupying 4k which constructor needs to open a file 200k and search for a string, or even worst, a vector of 100 of such objects.

    A reference will simply refer to the actual object and not create anything.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    A good general rule is to pass built-in types by value and user defined objects by reference to const.

    You can include simple structs that are comprised of only built-in types as candidates for pass-by-value as well.

  5. #5
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    I see. I understand now. I've never passed a parameter by reference except if I needed to change that parameter's value. Didn't give a damn about the memory allocation and copy speed. Thanks.

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, 09:18 AM

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