1

e.g. Person(const string & aName){...}

Would a program use up a significant amount of memory just because we don't pass by reference? I know how to use const-reference but am having trouble understanding why I'm using them.

Justin
  • 24,288
  • 12
  • 92
  • 142
user1692517
  • 1,122
  • 4
  • 14
  • 28
  • 3
    It's not the memory (usually) so much as it is the time taken copying the object. – Steve Nov 01 '17 at 23:19
  • You probably won't use up a significant amount of memory by not passing by reference. Instead, you'd have to go through the entire string and copy the data, only to not use anything that required a copy. As a real-life example, I wrote some code where I inadvertently had a single useless copy that *all* of my data went through, and my program (which runs pretty fast anyway) was about half as fast as when I fixed it – Justin Nov 01 '17 at 23:20
  • using const is for data security so you don't change the value. Also passing by reference allows you to save memory. – O2Addict Nov 01 '17 at 23:22
  • You are passing by reference to indicate that a copy will not be made. The reference is `const` because the interface says the referrant will not be modified. Also, using a reference says that the item already exists, whereas passing a pointer doesn't satisfy that guarantee. – Thomas Matthews Nov 01 '17 at 23:24
  • Passing by reference is a performance improvement, as there is no copy. Passing by `const` reference has two effects: it constrains the method implementation not to change the object, and it allows the compiler to construct temporaries at the call site, which can simplify the calling code. – user207421 Nov 01 '17 at 23:30

1 Answers1

4

Would a program use up a significant amount of memory just because we don't pass by reference?

A copy of the string (that would be made if you passed the parameter by value [unless the call is expanded inline, in which case it matters not how the parameter is passed]) would use as much memory as the original string. Thus such program would use twice as much memory for the string and its copy as another program would use for the one string that wasn't copied.

The difference is not limited to the amount of used memory. Copying memory takes more time than not copying memory. The overhead from passing by reference is constant both by speed and memory use, while the overhead of copying a string increases linearly as the length of the string increases.

The linear complexity of copying the string can be significant if the string is not guaranteed to be small. Even if the string is small, the allocation overhead can be significant if the function is called often.

What is the significance of passing const-reference parameters?

When you use a const reference rather than non-const, it will be easier to reason about the correctness of your program, since you implicitly know just by the declaration of the function, that it will not modify the referred object. It also allows you to pass const objects, and temporaries to the function.

Passing by values has those same advantages, but it requires a copy which can have significant performance impact as I explained in earlier paragraph. If the function itself would make a copy of the string anyway, then it may be better to pass by value, so that the copy is apparent to the reader.

eerorika
  • 232,697
  • 12
  • 197
  • 326