Jump to content
  • Advertisement
Sign in to follow this  
Timberl

Thinking about C++ references

This topic is 4985 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Having let the idea of references simmer round in my head for a while I've thought of a few things which I'd like to run by you guys. if you pass by reference, you will generally never lose anything in terms of speed over passing by value, in fact you will probably gain speed because you don't have an internal copy command. if you dont want the original changed, you can can const the reference and will then need to make a local copy in your function if you want to 'play' with the passed argument. But surely this is just the copy that would have got done anyway passing by value, so in effect passing by reference is never going to be slower than passing by value. So is pass-by-value just for the programmers convinience now? and therefore is it fair to say your programs would run as fast if not faster if you never passed by value and always used reference. And therefore might it be good policy to always pass by reference in place of passing by value? ?

Share this post


Link to post
Share on other sites
Advertisement
For all practical purposes, that's the method Java uses*. Personally, I don't like it, for readability purposes. I dislike functions that modify its arguments, so I prefer to assume they won't unless explicitly told otherwise. Defaulting to pass-by-value fits this scheme better.

CM

*Yes, yes, its passing a reference by value. Blah blah blah, either way you're passing a reference. No nit-picking.

Share this post


Link to post
Share on other sites
Sounds like a theory argument waiting to happen. =)

I often pass by reference because it saves on overhead generated which, yes, does save on some speed...especially for methods/functions you call a lot. Other than backwards compatibility I honestly don't know of an advantage you get out of passing into a copy. Maybe so you can screw with the parameter value directly, like for a recursive function.

I'd be curious to see what some of the more experienced people on the boards have to say.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Conner McCloud
For all practical purposes, that's the method Java uses*. Personally, I don't like it, for readability purposes. I dislike functions that modify its arguments, so I prefer to assume they won't unless explicitly told otherwise. Defaulting to pass-by-value fits this scheme better.

CM

*Yes, yes, its passing a reference by value. Blah blah blah, either way you're passing a reference. No nit-picking.

Actually Java passes a pointer by value, not a reference. That's why you can call a function with null as parameter.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by Conner McCloud
For all practical purposes, that's the method Java uses*. Personally, I don't like it, for readability purposes. I dislike functions that modify its arguments, so I prefer to assume they won't unless explicitly told otherwise. Defaulting to pass-by-value fits this scheme better.

CM

*Yes, yes, its passing a reference by value. Blah blah blah, either way you're passing a reference. No nit-picking.

Actually Java passes a pointer by value, not a reference. That's why you can call a function with null as parameter.

You're nit-picking. A pointer is a reference that doesn't neccessarily reference anything. Now, if we could please stop playing word games?

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Timberl
if you pass by reference, you will generally never lose anything in terms of speed over passing by value, in fact you will probably gain speed because you don't have an internal copy command.


That may be true given a very clever compiler (and linker) with complete knowledge about the whole program. While passing by (const) reference frequently make sense, in practice there are cases where passing by reference will be less efficient.

The most obvious problem is pointer-sized (or smaller) arguments: unless the compiler is able to optimize away the implicit pointer, copying the value will be cheaper than passing a pointer and then dereferencing it in the function. A less obvious efficiency problem is pointer aliasing due to references.

Share this post


Link to post
Share on other sites
I thought references were already dereferenced? (my knowledge here is in its infancy so I'm not certain)

I'm not arguing the use of pointers over values - there are clear-cut pros & cons to each and the smaller-datatype-than-pointer argument does hold true (even though I personally find it uncompelling). This is strictly references vs. values

Share this post


Link to post
Share on other sites
Quote:
Original post by Timberl
I thought references were already dereferenced?


Aren't they values, then? ;-)

References are little more than non-NULL pointers in disguise. They have certain semantics that pointers do not, but the two are pretty much the same thing.

Share this post


Link to post
Share on other sites
Quote:
Original post by Timberl
And therefore might it be good policy to always pass by reference in place of passing by value?


If you're going to pass by reference, and then make a copy because you need to modify the variable in the function, but not the original, you might as well pass by value.

And, just in case, Have you checked this thread?

Share this post


Link to post
Share on other sites
Quote:
Original post by spock
Quote:
Original post by Timberl
I thought references were already dereferenced?


Aren't they values, then? ;-)

References are little more than non-NULL pointers in disguise. They have certain semantics that pointers do not, but the two are pretty much the same thing.

Totally agree. Internally, reference is just pointer. With some syntactic shugar and some restrictions, that is there mainly(if not only) because using * as dereference operator is bad for readability.

Reference itself is passed to function by value. That is, in reality, pointer is passed to function by value. If your argument is smaller than pointer, it's better to pass argument itself if you don't want to modify it.If you want to modify, reference is usually better than pointer. If you want to "play" with argument, it's better to pass it by value. If you don't want to modify and don't want to play, but argument is bigger than pointer, it's better to pass by const reference.

Smart compiler might be able to optimize out all that, but only if function is "inline" (used only inside this library). Most compilers can't do _any_ even simple optimizations across libraries (across h+cpp modules) , principially * .


* And it's one of reasons why C++ is not a "fastest language" and "c++ is designed to be faster" is not correct - compatibility had bigger priority than performance... still, if accurately coded, C++ is fast.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!