Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created November 22, 2009 09:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/240499 to your computer and use it in GitHub Desktop.
Save JoshCheek/240499 to your computer and use it in GitHub Desktop.
Testing the differences between references and pointers
*PURPOSE:
This is a test to evaluate the underlying difference between references and pointers.
*PROCESS:
The session can be seen in this image http://grab.by/Jbi
First:
Create a file that uses references (references.cpp), then duplicate it using pointers(pointers.cpp)
Second:
Using the -S flag, we can tell the compiler to generate the assembly source for the references
(references.s) and the pointers (pointers.s)
Third:
Perform a diff on the files to see what the differences are. (references_vs_pointers.diff)
*CONCLUSION:
Looking at the diff, we can see that the files generated are exactly the same with the exception of
the naming conventions, the references use the capitol letter R in their names (__Z3fooRi) while the
pointers use the capitol letters P in their names (__Z3fooPi). However, aside from the difference in
names, the code is exactly the same. This indicates to me that references are pointers with a
convenient syntax, and restricted functionality. Essentially, pointers with syntactic sugar to impart
to the programmer a way of thinking.
*INTERPRETATION:
This is not a criticism of references, just an attempt to better understand them. Changing the ease
through which something is accomplished, the way it is accessed, the way it behaves changes how people
think, which changes how they approach problem solving. References are one tool through which this can
be accomplished.
Some quotes to instantiate my thoughts:
Languages help programmers to think and the language gives inspiration to programmers
- Yukihiro Matsumoto in a presentation on programming languages http://www.youtube.com/watch?v=ix2DeCzuckc
“there is a systematic relationship between the grammatical categories of the language a
person speaks and how that person both understands the world and behaves in it.”
For my purposes that means that the language you use shapes how you think... and if you want
to change how you think it can help to first change your language.
- Dave Astels in his essay on Behaviour Driven Development http://blog.daveastels.com/files/BDD_Intro.pdf
void foo( int *bar ){ ++*bar; }
int main( void ){ int n=0; foo(&n); return 0; }
.text
.align 1,0x90
.globl __Z3fooPi
__Z3fooPi:
LFB2:
pushl %ebp
LCFI0:
movl %esp, %ebp
LCFI1:
subl $8, %esp
LCFI2:
movl 8(%ebp), %eax
movl (%eax), %eax
leal 1(%eax), %edx
movl 8(%ebp), %eax
movl %edx, (%eax)
leave
ret
LFE2:
.align 1,0x90
.globl _main
_main:
LFB3:
pushl %ebp
LCFI3:
movl %esp, %ebp
LCFI4:
subl $40, %esp
LCFI5:
movl $0, -12(%ebp)
leal -12(%ebp), %eax
movl %eax, (%esp)
call __Z3fooPi
movl $0, %eax
leave
ret
LFE3:
.globl __Z3fooPi.eh
__Z3fooPi.eh = 0
.no_dead_strip __Z3fooPi.eh
.globl _main.eh
_main.eh = 0
.no_dead_strip _main.eh
.constructor
.destructor
.align 1
.subsections_via_symbols
void foo( int &bar ){ ++bar; }
int main( void ){ int n=0; foo(n); return 0; }
.text
.align 1,0x90
.globl __Z3fooRi
__Z3fooRi:
LFB2:
pushl %ebp
LCFI0:
movl %esp, %ebp
LCFI1:
subl $8, %esp
LCFI2:
movl 8(%ebp), %eax
movl (%eax), %eax
leal 1(%eax), %edx
movl 8(%ebp), %eax
movl %edx, (%eax)
leave
ret
LFE2:
.align 1,0x90
.globl _main
_main:
LFB3:
pushl %ebp
LCFI3:
movl %esp, %ebp
LCFI4:
subl $40, %esp
LCFI5:
movl $0, -12(%ebp)
leal -12(%ebp), %eax
movl %eax, (%esp)
call __Z3fooRi
movl $0, %eax
leave
ret
LFE3:
.globl __Z3fooRi.eh
__Z3fooRi.eh = 0
.no_dead_strip __Z3fooRi.eh
.globl _main.eh
_main.eh = 0
.no_dead_strip _main.eh
.constructor
.destructor
.align 1
.subsections_via_symbols
3,4c3,4
< .globl __Z3fooRi
< __Z3fooRi:
---
> .globl __Z3fooPi
> __Z3fooPi:
33c33
< call __Z3fooRi
---
> call __Z3fooPi
38,40c38,40
< .globl __Z3fooRi.eh
< __Z3fooRi.eh = 0
< .no_dead_strip __Z3fooRi.eh
---
> .globl __Z3fooPi.eh
> __Z3fooPi.eh = 0
> .no_dead_strip __Z3fooPi.eh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment