2

Possible Duplicate:
C++: How do I decide if to pass params by ref or by value?

The following function has been written from C++ Primer, 5th Edition Page 211 and Page 214. This function will return the position of the first occurrence of a given character in a string and tell the number of occurrences of that character in that string.

string::size_type find_char(const string &s, char c, string::size_type &occurs)
{
      // Compares the given character with string 
      // Records the first occurrence of that character
      // The change in &occurs is reflected back to the original variable
}

The authors recommend the use of 'References to Avoid Copies' when passing parameters and the use of 'const reference parameters' for parameters that the function doesn't change. Why didn't they make char c a const reference parameter?

Community
  • 1
  • 1
chosentorture
  • 329
  • 2
  • 13
  • You may want to see a few of the other posts on this, such as http://stackoverflow.com/questions/9442202/c-how-do-i-decide-if-to-pass-params-by-ref-or-by-value . Some of them are pretty good. I particularly like this one: http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c – WhozCraig Jan 26 '13 at 10:27

2 Answers2

5

Why didn't they make char c a const reference parameter?

char is so small, it's generally cheaper to pass it by value rather than by reference (const or otherwise).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • So should the user risk the declaration of a character as a non `const` reference when he doesn't want it to be changed? – chosentorture Jan 26 '13 at 10:46
  • 1
    @ChosenTorture: Passing `char&` really only makes sense if the function wants to be able to change the value as seen by the caller. – NPE Jan 26 '13 at 10:49
  • Making the string a `const` reference and omitting it for char when both of them are used for the same purpose; is this done only because char is cheaper to pass by value? – chosentorture Jan 26 '13 at 11:03
  • 3
    @ChosenTorture this may be said, that it is an established convention to *always* pass primitive types by value. By adding `conts &` to a primitive type parameter (char, int, double...) you get **nothing**, only less-readalbe code. But you ask why it is done for `string` and is not done for `char`? I think this difference is caused by the fact that *types are different*. `char` is primitive, `string` is *not*. – NIA Jan 26 '13 at 11:38
  • P.s. By "`srting` is not primitive" I mean *NOT* that `sizeof(string)` is much more than `sizeof(char)` — the issue is not about stack usage. Passing `string` by value will cause **calling its copy-constructor**, which will result in unnecessary allocating of heap memory for dublicate of this string, and in wasting of time for copying it. This is not important if `string` if `"abc"`, but if it is, say, a complete webpage source... You see. In case of passing `char` by value **none of this overhead** happens. – NIA Jan 26 '13 at 11:43
1

As @NPE states, char, like all built-in types, can be passed by value as easily as a pointer or reference.

Technically, while there's no speed difference in creating a pointer or reference to a built-in type and passing by value, there is, however, a performance hit when dereferencing that pointer or reference to access the underlying value. The compiler does have some optimisation strategies available where references are concerned that might mean it's no quicker to pass by value. But generally speaking, you shouldn't rely on what the optimiser might or might not do. Most of the time it wont do what you think.

The general rule of thumb is pass built-in types by value, and user defined types by reference (or pointer). There are circumstances which will force you to break this rule, but you will know them when you encounter them.

  • Making the string a `const` reference and omitting it for char when both of them are used for the same purpose; is this done only because char is cheaper to pass by value? – chosentorture Jan 26 '13 at 11:10