-3
void test(const int* pInt)

or

void test(int pInt)  

I know that the first example makes pInt some kind of protected. But which solution provides more performance? Since both (the const pointer and the normal int) have to be registered in the memory.

user1511417
  • 1,880
  • 3
  • 20
  • 41
  • Did you mean `void test(int *pInt)` in your second example? – Martin Green Feb 16 '13 at 16:10
  • No! I really mean a simple int – user1511417 Feb 16 '13 at 16:12
  • This is very basic. They are two different types and it's as simple as that. Which C++ book are you using? – Lightness Races in Orbit Feb 16 '13 at 16:39
  • 1
    @user1511417: First of all, that's not a book. Second, it's _awful_. It contains all sorts of errors and misconceptions. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Lightness Races in Orbit Feb 16 '13 at 16:46
  • @LightnessRacesinOrbit: I implore you to take another look at cplusplus.com. I'm not saying it doesn't have errors, but they've made a lot of improvements recently. For example, many (if not all) of the complaints in [this thread](http://stackroulette.com/programmers/88241/undefined), are no longer applicable. – Benjamin Lindley Feb 16 '13 at 17:15
  • @BenjaminLindley: I almost did, but then I found out this week that _their_ tutorials are the ones teaching language newcomers to incorrectly use `istream::eof` in loop conditions. That plus the repeated belief from said newcomers that cplusplus.com is somehow "_the_ [official] resource for C++" makes me want to gouge my eyeballs out whenever it's refered to. – Lightness Races in Orbit Feb 16 '13 at 17:25
  • The question which book I use is off-topic anyway! – user1511417 Feb 16 '13 at 17:30

3 Answers3

1

Technically, the second can be faster because the first is subject to aliasing - there's no way to tell whether pInt isn't modified outside the function. Just because pInt is const inside the method, it doesn't mean the original variable passed as argument has to be const - remember non-const to const conversion is implicit.

A workaround for this issue would be using _restrict if supported by the compiler. Note that this is an intrinsic and not part of the language.

In most usage though, they'll be the same. Profile profile profile. Write the code for readability, and only do these small optimizations when and if you know they're worth it.

This applies to int - if you're passing a a large structure, passing by value can be a bottleneck if copy elision can't be applied to that particular case. So passing a pointer would be faster, but I'd still use a reference.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    Your answer makes me even more confused. I used "const" here, because I want the pointer to behave like the normal int (sec. example), which means that in both examples pInt can not be changed inside the function! – user1511417 Feb 16 '13 at 16:23
  • @user1511417 inside the function, no. But what about outside, in a multi-threaded environment? – Luchian Grigore Feb 16 '13 at 16:28
  • 1
    @user1511417 You have some fundamental confusion here. In the case of `const int *pInt` you're using the same variable as the caller. In case of `int pInt` you get a **copy** of callers value. Using a pointer *could* be faster then making a copy, but only in case of significantly larger types, and not simple `int`'s. – Martin Green Feb 16 '13 at 16:29
  • @ Luchian I don't care what happens outside the function. I just want to pass values here. If I want to change "pInt"s value (for the outside), I simply leave out the "const" – user1511417 Feb 16 '13 at 16:30
  • @user1511417 but that only prevents changing the value inside the function. The pointer can still be changed outside. – Luchian Grigore Feb 16 '13 at 16:31
  • @user1511417 You're not passing a value of `int` if you're using `const int *pInt`. – Martin Green Feb 16 '13 at 16:31
  • @Luchian that's correct! that's what I mean! – user1511417 Feb 16 '13 at 16:32
  • @MartinshShaiters your first comment is perfect! thanks for that! Concerning your sec. comment: not directly. I'm pointing to an int without being able to change it inside my function – user1511417 Feb 16 '13 at 16:35
  • @Martinsh: `In the case of const int *pInt you're using the same variable as the caller` Seems like _you_ have some fundamental confusion. The pointer argument is copied just like any other object. – Lightness Races in Orbit Feb 16 '13 at 16:40
  • 1
    @LightnessRacesinOrbit Poor wording on my part. I know that the value of the pointer (address it points to) is copied. What I mean is that, in former case you'd end up accessing the same pointed-to `int` in the callee as in the caller, and in the later case the callee will have a local copy of `int` parameter. – Martin Green Feb 16 '13 at 16:45
  • @MartinshShaiters can you formulate an answer to my question/topic here? I like what you wrote so far. – user1511417 Feb 16 '13 at 16:49
  • @user1511417 lol, I'm not saying he's wrong, but just because you *like* what someone is saying, that doesn't it what you should take it for granted. – Luchian Grigore Feb 16 '13 at 16:56
  • 1
    @user1511417: You think you mean that, but you actually don't mean that. Because you said *"I just want to pass values here."*. That clearly demonstrates that you *do* care if the value changes outside the function, you just don't realize it, because you are not understanding what Luchian is saying. So let me rephrase it. Are you okay with the idea that the value of the int might change *while* your function is running? Because that can happen if you use the pointer, *even* if you use const. – Benjamin Lindley Feb 16 '13 at 16:56
1

Depends on what you do with the parameter inside the function. If you're very frequently de-referencing the pointer, you're indirect and will be doing more work than had you just passed the value as a variable.

Do you believe this choice causes a performance tradeoff in your application? What is it that you're doing in your function implementation, and why do you think one might be materially faster than the other?

MikeB
  • 1,452
  • 14
  • 28
0

both examples pInt can not be changed inside the function!

Then you forgot a const to the second function. Realize that you can change pInt value as you wish in the second function, however since the whole thing was passed as value any changes there won't affect anything else.

Don't you want to do this?

void test(const int pInt)

If you do that, you will have a const int to your function (that was passed as argument).

Alien
  • 52
  • 3