Discussion:
Does the compiler generate better code for references than pointers?
(too old to reply)
Stephen Howe
2009-11-19 15:40:43 UTC
Permalink
Subject says it all

Using VS2005
Does the compiler generate better code for references than pointers?

I half think so (because it does not have to worry about NULL) but would interested in any links confirming this.

Thanks

Stephen Howe
David Lowndes
2009-11-19 16:16:25 UTC
Permalink
Post by Stephen Howe
Using VS2005
Does the compiler generate better code for references than pointers?
I half think so (because it does not have to worry about NULL) but would interested in any links confirming this.
Stephen,

Given that the compiler doesn't have to worry about NULL for pointers
either, I'd assume any difference would be negligible.

Why don't you come up with an example usage and compile with the
assembly listing and check it.

Dave
Igor Tandetnik
2009-11-19 16:23:16 UTC
Permalink
Post by Stephen Howe
Subject says it all
Using VS2005
Does the compiler generate better code for references than pointers?
I half think so (because it does not have to worry about NULL) but
would interested in any links confirming this.
Well, it generally doesn't have to worry about NULL with pointers either - _you_ have to.

Some optimizations could be gleaned from the fact that references can't be reseated (assigned to point to something else). In many cases, the compiler doesn't need to actually allocate memory for a reference, but just treat it as an alias for the referent. However, an optimizing compiler should be able to optimize away a pointer under similar circumstances (the pointer is initialized to point to an object, and is provably never modified until destroyed).
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Continue reading on narkive:
Loading...