0

I've got a simple question. This is my C++ code:

#include <iostream>
using namespace std;

void example(const int someArgument)
{
    cout << someArgument << endl;
}

int main()
{
    int someArgument = -1;
    example(someArgument);
}

Does running example(someArgument) makes a copy in memory of someArgument, or passes only address to variable? I assume that compiler "knows" that I won't modify it by using const keyword so it shouldn't be necessary to make a copy. Am I wrong?

Guy Avraham
  • 3,156
  • 3
  • 36
  • 46

2 Answers2

5

You are passing by value so, yes, it will make a copy.

If you don't want to make a copy, pass the argument by pointer or reference. In this case, though, you are passing an int so I would not worry about passing by value.

My rule of thumb is to pass primitive types by value, all others by const reference if possible.

Anon Mail
  • 4,575
  • 1
  • 17
  • 20
  • 2
    I would recommend against sharing contentious rules of thumb such as this one as part of an answer. – François Andrieux Apr 18 '17 at 14:34
  • Let's assume that I'm passing a std::string. How does this change the game? Even using -O3 g++ argument wouldn't make the compiler optimize it how it should be? – MobileDevelopment Apr 18 '17 at 14:35
  • That rule of thumbs doesn't really add for anything. there's a really good blogpost about this here: http://www.macieira.org/blog/2012/02/the-value-of-passing-by-value/ – Tomaz Canabrava Apr 18 '17 at 14:42
  • 1
    OK, maybe you wanted to mention that int is smaller than a pointer on, f.e. x86 or AMD64, so it is better to pass it as copy? – MobileDevelopment Apr 18 '17 at 14:42
  • 1
    @MobileDevelopment It depends on what you *do* with your string. Are you going to modify it? Are you going to store it for later? From your example, it seems like you will simply be reading the passed value. If you are going to pass by large objects by const value, you are usually better off passing by const reference instead. Why hope the compiler will preform the optimization when you can enforce it yourself with no significant downside? – François Andrieux Apr 18 '17 at 14:43
  • 1
    @MobileDevelopment Technically, yes. `int` is small enough to be passed in a register, so passing by value would (in theory) be faster than making a reference, and passing that. – Rakete1111 Apr 18 '17 at 14:44
  • one may take in account memory caching as well...but we aren't coding in assembler – Swift - Friday Pie Apr 18 '17 at 14:50
0

the keyword const is not what tells you about making copies or not of that variable, you have basically 3 options...

void example(const int someArgument)

void example(const int& someArgument)

void example(const int* someArgument)

the one you have is thmaking a copy of the argument you give and writes into someArgument variable..

so with void example(const int someArgument) you are passing by value an integer, so is a copy (that you can not change since is const)

ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91