0

The variables are used in a read-only manner in the function I pass them to (so they are const)

It is known that passing by reference is better for complex objects since it avoids copying (wich induces a whole hierarchy of calls to constructors). However for an int does it make sense ? In general references are implemented as pointers (I heard) so It would take more memory to handle them since a pointer is typically 8 bytes length and an Int is 4 bytes (on a typical 64 bits platform).

user2370139
  • 1,297
  • 1
  • 11
  • 13
  • related/dupe: https://stackoverflow.com/questions/3009543/passing-integers-as-constant-references-versus-copying probably many more. Another one: https://stackoverflow.com/questions/4143644/c-64-bit-int-pass-by-reference-or-pass-by-value – NathanOliver May 22 '17 at 12:59

2 Answers2

3

Does passing by reference primitive types such as int, bool gives any speedup over passing by value ?

No.

Or does it worsen the time/space usage?

Potentially yes.

so It would take more memory to handle them since a pointer is typically 8 bytes length and an Int is 4 bytes

Correct. Although to be exact, neither the size of int nor the size of pointers are specified by the standard. On some systems your statements may be accurate.

However, if the function call is expanded inline, this won't matter at all.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Passing primitives by value is generally faster and considered good practice. The compiler has to worry about things like aliasing when you pass references, so it becomes more difficult to optimize references to built in types than the built in types themselves.

Jayson Boubin
  • 1,504
  • 2
  • 11
  • 19