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,482
  • 3
  • 38
  • 50
  • 4
    [`using namespace std;` is a bad practice](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – tambre Apr 18 '17 at 14:29
  • 4
    This is something like pseudocode, just ignore it. – MobileDevelopment Apr 18 '17 at 14:30
  • why do you care? If it is `const` it doesnt really matter if it is a copy or not, and then the compiler may apply any optimizations it likes under the as-if-rule – 463035818_is_not_an_ai Apr 18 '17 at 14:30
  • 3
    The `const`ness of arguments doesn't impact rather or not a copy is made. The fact that it's taken by value is what makes a copy. – François Andrieux Apr 18 '17 at 14:30
  • So, what's the purpose of being const-correct? – MobileDevelopment Apr 18 '17 at 14:36
  • 1
    @MobileDevelopment To not modify a value accidentally. – Rakete1111 Apr 18 '17 at 14:37
  • 1
    @MobileDevelopment `const` and const-correct programming are important error mitigation tools and, in some cases, facilitate compiler optimizations. They allow the compiler to yell at you when you make certain easy-to-miss mistakes. It also helps produce more expressive code for other programmers or for your future self. – François Andrieux Apr 18 '17 at 14:47

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,660
  • 1
  • 18
  • 21
  • 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 ツ
  • 47,427
  • 17
  • 69
  • 97