Thread: are "const int&" parameters useful?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    28

    are "const int&" parameters useful?

    hi guys

    i was wondering if i should rigorously stick to passing by reference.

    i was wondering if someone knew what kind of effect passing a const int by reference would have? ( what about doubles or shorts? )

    does it also make my programs perform faster or would it make them slower?
    Last edited by symbiote; 03-14-2009 at 12:13 PM.

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    It depends, are you passing just one integer, or double, or are you passing 500,000 integers, or doubles?

    When you pass by value a copy of what your are passing to the function is made in memory. If what you are passing is large it would take a while to copy all of that information.

    But for just a single, or few, integers or doubles, that copying won't effect performance.

    Passing by const reference ensures that the functions being called do not change the contents of what is being passed.

    Hope I said all of the right.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    28
    thanks for the response Phyxashun,
    i see.

    what i'm wondering now is, whether there is a cost in passing an int by reference.
    - and if it exist, wheter it would be more or less than the copy of the int

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by symbiote
    what i'm wondering now is, whether there is a cost in passing an int by reference.
    - and if it exist, wheter it would be more or less than the copy of the int
    It is likely to be the same cost as passing a pointer, since references are typically implemented as const pointers. Where the size of a pointer is equal to the size of an int, the cost would then be in pointer dereference. Either way, it is a marginal cost.
    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

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think the benefit of passing by reference only comes when the object being passed is larger than a pointer. So passing primitive data types like int, char... by reference probably wouldn't make a difference, but passong structs & classes would make a noticable difference.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    I think the benefit of passing by reference only comes when the object being passed is larger than a pointer. So passing primitive data types like int, char... by reference probably wouldn't make a difference, but passong structs & classes would make a noticable difference.
    There are other effects, though. If the object being passed is an extremely small object (say, the size of an integer), the pass-by-value will cause a copy constructor to be invoked, whereas passing by reference will not. So that's a measurable change in behavior.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    There are other effects, though. If the object being passed is an extremely small object (say, the size of an integer), the pass-by-value will cause a copy constructor to be invoked, whereas passing by reference will not. So that's a measurable change in behavior.
    But of course, for standard C++ basic types such as int, the built-in copy constructor is a simple move instruction [or something to that effect], which is about as little as could possibly be.

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

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    28
    thank you for your insight everyone!
    i think i don't think i'll be passing primitive types by reference for reasons.

    Greetings

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parameters quick Question
    By lifeis2evil in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2007, 11:12 PM
  2. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  3. Additional parameters for operator delete
    By darksaidin in forum C++ Programming
    Replies: 0
    Last Post: 09-21-2003, 11:46 AM
  4. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  5. command-line parameters.
    By Tombear in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 08:40 AM