What's the low-level difference between a pointer an a reference?

后端 未结 4 1974
悲哀的现实
悲哀的现实 2020-12-16 17:13

If we have this code:

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

There\'s no actual binary difference in the ref

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 17:35

    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.

提交回复
热议问题