Page 1 of 1

& vs. *

Posted: Fri Jul 10, 2009 9:58 am
by hurstshifter
Hey Guys,

So I have question which may sound a bit amateur but please bear with me (as I am an amateur :) ). I just started diving more deeply into OO programming with C++ and have recently been utilizing references. I've always utilized pointers and admittedly have had problems utilizing them the way I really wanted to in the past. Since learning about references, all those problems have pretty much disappeared. My main question is this...

What are the benefits of using a pointer to a variable rather than a call by reference?

It seems to me that they work in a similar manner (although I do understand the differences between the two). References appear to be a bit more efficient and just are cleaner overall when using them in a program. If you could provide some examples as to when it would be a better idea to use one over the other that would be awesome. I am ready to be ejumakated.

Re: & vs. *

Posted: Fri Jul 10, 2009 11:44 am
by short
One benefit of a pointer is that you can manipulate nearby memory addresses.

If you have:

int i = 5;
int * c = &i;
c++;


now c points to the memory address one place above (below?) where i is in memory.


edit: I know you can do this with stack allocated variables, but I am not so sure with heap-allocated variables if you can do this. It might be OS dependent, or language dependent, or not possible at all :lol:

edit: edited retarded error, lol.

Re: & vs. *

Posted: Fri Jul 10, 2009 12:00 pm
by Netwatcher
pointers can point multiple objects in their lifetime, while reference can only ref one.

if you have:

decleration

Code: Select all

int i;
int *pti= &i;
int &rti = i;
to the pointer you can do

Code: Select all

pti++
and move freely in the memory...

assigning value

Code: Select all

*pti = 10;
vs

Code: Select all

rti = 10;
btw nice pun
hurtshifter wrote:So I have question which may sound a bit amateur

Re: & vs. *

Posted: Fri Jul 10, 2009 12:42 pm
by K-Bal
short0014 wrote: int i = 5;
int * c = i;
Hmm, ERROR ? ;)

Code: Select all

int i = 5;
int * c = &i;

Re: & vs. *

Posted: Fri Jul 10, 2009 1:34 pm
by short
lol, i think I was still hung over when I wrote that code this morning.

THanks :lol:

Re: & vs. *

Posted: Fri Jul 10, 2009 3:28 pm
by hurstshifter
I see. So references are like a one woman man, while pointers don't mind jumping around from address to address.

Re: & vs. *

Posted: Fri Jul 10, 2009 3:33 pm
by Netwatcher
hurstshifter wrote:I see. So references are like a one woman man, while pointers don't mind jumping around from address to address.
:lol:
hmm... more like you are the pimp and you tell the pointer what address he needs to be on

Re: & vs. *

Posted: Fri Jul 10, 2009 5:48 pm
by programmerinprogress
As vince the shamwow guy would say if he had a f****** clue about pointers and references!
with regular cloths you can only use them once (References), bim bam bow!, but with shamwow (pointers) you can clean up those spills eveyday! you're gonna love this! also, pointers are made in Germany whereas References are made in Russia, BIM BAM POW, BUY THIS NOW!
if you don't have a clue what i'm going on about, explore these videos at your leisure :lol:

Lesson 1: Basic Vince talk
http://www.youtube.com/watch?v=QwRISkyV_B8
Lesson 2: Introducing the Slap Chop
http://www.youtube.com/watch?v=rUbWjIKxrrs
Lesson 3: Advanced Vince offer rythms (Rap Chop)
http://www.youtube.com/watch?v=UWRyj5cH ... re=related

Enjoy :P

Re: & vs. *

Posted: Fri Jul 10, 2009 8:49 pm
by derbon
what??

Re: & vs. *

Posted: Fri Jul 10, 2009 9:39 pm
by eatcomics
I love rap chop... "You're gonna love my nuts!"

BTW that was the best analogy ever....

Re: & vs. *

Posted: Sat Jul 11, 2009 10:29 pm
by Falco Girgis
References are implemented at compile time as auto dereferenced pointers. Thinking of them in that light has always helped me.

Re: & vs. *

Posted: Sun Jul 12, 2009 3:53 am
by Netwatcher
programmerinprogress wrote:As vince the shamwow guy would say if he had a f****** clue about pointers and references!
BIM BAM POW, BUY THIS NOW!
OMG... Dr.seuss and the ShamWoW guy... ewww

Re: & vs. *

Posted: Mon Jul 13, 2009 7:45 am
by hurstshifter
GyroVorbis wrote:References are implemented at compile time as auto dereferenced pointers. Thinking of them in that light has always helped me.
That definitely helps