views:

456

answers:

4

If we have this code:

int foo=100;
int& reference = foo;
int* pointer = &reference;

There's no actual binary difference in the reference's data and the pointer's data. (they both contain the location in memory of foo)

part 2

So where do all the other differences between pointers and references (discussed here) come in? Does the compiler enforce them or are they actually different types of variables on the assemebly level? In other words, do the following produce the same assembly language?

foo=100;
int& reference=foo;
reference=5;

foo=100;
int* pointer=&foo;
*pointer=5;
+12  A: 

Theoretically, they could be implemented in different ways.

In practice, every compiler I've seen compiles pointers and references to the same machine code. The distinction is entirely at the language level.

But, like cdiggins says, you shouldn't depend on that generalization until you've verified it's true for your compiler and platform.

Crashworks
In practice however, compilers often elide references entirely, especially when they're locals.
Pavel Minaev
Pointers too sometimes, if they can.
Crashworks
So, exactly what combination of machine architecture, compiler, and optimization settings have this held true for?
cdiggins
MSVC, GCC, Metrowerks, suncc, Intel across x86-32, x86-64, PPC, Emotion Engine, Cell/SPU, Xenon, Moto, SPARC, MIPS.
Crashworks
Oh, and whatever crappy compiler SGI shipped with IRIX for their Indys.
Crashworks
With all honesty, I am damn impressed! Still I hold my original point: guessing at machine code is just asking for trouble unless your architecture and compiler are fixed.
cdiggins
A: 

Pointers and references have different semantics in C++, but the code generated is the same.

nschmidt
Nothing in the C++ specification requires this.
cdiggins
It could be different of course, but it's probably simpler to implement this way.
Jurily
@cdiggins: You're right, but practically when you use a reference in a context where you could use a pointer, you will very likely get the same code.
nschmidt
+6  A: 

There is absolutely nothing reliable about the relationship between C++ code and what machine code a compiler generates.

Some people say "in my experience ... etc. etc. etc." but this is more unreliable than you may realize. Not many people have actual experience in any substantial cross-section of all the possible compiler/architecture combinations. [Edit: I think that Crashworks proves me wrong though. :-)]

Consider the following list of C++ compilers:

  • C++ Builder
  • Turbo C++ Explorer
  • C++ Compiler
  • Borland C++
  • Turbo C++ for DOS
  • Clang
  • Comeau C/C++
  • CoSy compiler development system
  • Digital Mars
  • Djgpp
  • EDGE ARM C/C++
  • MinGW
  • GCC C++ (G++)
  • HP aC++
  • Intel C++ Compiler
  • Microtec
  • MULTI
  • Open Watcom
  • Open64
  • PathScale
  • PGI Workstation
  • ProDev WorkShop
  • RealView C/C++ Compiler (armcc)
  • SAS/C C++
  • Sun Studio
  • TenDRA
  • VectorC
  • Visual C++
  • VisualAge C++
  • XL C/C++

Now multiply this list by the following short list of machine architectures:

  • ARM
  • Atmel AVR
  • Blackfin
  • HC12
  • H8/300
  • IA-32 (x86)
  • x86-64
  • IA-64
  • Motorola 68000
  • MIPS
  • PA-RISC
  • PDP-11
  • PowerPC
  • R8C/M16C/M32C
  • SPU
  • System/390/zSeries
  • SuperH
  • SPARC
  • VAX
  • A29K
  • ARC
  • ETRAX CRIS
  • D30V
  • DSP16xx
  • FR-30
  • FR-V
  • Intel i960
  • IP2000
  • M32R
  • 68HC11
  • MCORE
  • MMIX
  • MN10200
  • MN10300
  • Motorola 88000
  • NS32K
  • ROMP
  • Stormy16
  • V850
  • Xtensa
  • AVR32

Now multiply by operating system and optimization flags, and you may find that everyone's experience is woefully lacking.

cdiggins
You're name was familiar, and I remembered Heron :)
Will
I'm curious if you have a specific counterexample where pointers and references are implemented differently.
Crashworks
No. But it is actually surprises me that that I haven't seen any. There are surely some potential memory management optimizations that are available when the actually memory address is hidden from the user.
cdiggins
@Will: Have we met? Or have you just seen me blather on about Heron in different places?
cdiggins
A: 

Just to amplify, while it might be true that references are the same as pointers under the hood on nearly all compilers, it is a serious mistake to depend on that behavior. Not only is it likely to bite you on the ass when you least expect it, but it's also incorrect use of references. If it's a pointer you need, use a pointer.

ceo