Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Does the compiler generate better code for references than pointers?

1 view
Skip to first unread message

Stephen Howe

unread,
Nov 19, 2009, 10:40:43 AM11/19/09
to
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

unread,
Nov 19, 2009, 11:16:25 AM11/19/09
to
>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

unread,
Nov 19, 2009, 11:23:16 AM11/19/09
to

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

0 new messages