& vs. *
Moderator: Coders of Rage
- hurstshifter
- ES Beta Backer
- Posts: 713
- Joined: Mon Jun 08, 2009 8:33 pm
- Favorite Gaming Platforms: SNES
- Programming Language of Choice: C/++
- Location: Boston, MA
- Contact:
& vs. *
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.
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.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
http://www.thenerdnight.com
- short
- ES Beta Backer
- Posts: 548
- Joined: Thu Apr 30, 2009 2:22 am
- Current Project: c++, c
- Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
- Programming Language of Choice: c, c++
- Location: Oregon, US
Re: & vs. *
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
edit: edited retarded error, lol.
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
edit: edited retarded error, lol.
Last edited by short on Fri Jul 10, 2009 1:42 pm, edited 1 time in total.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
- Netwatcher
- Chaos Rift Junior
- Posts: 378
- Joined: Sun Jun 07, 2009 2:49 am
- Current Project: The Awesome Game (Actual title)
- Favorite Gaming Platforms: Cabbage, Ground beef
- Programming Language of Choice: C++
- Location: Rehovot, Israel
Re: & vs. *
pointers can point multiple objects in their lifetime, while reference can only ref one.
if you have:
decleration
to the pointer you can do
and move freely in the memory...
assigning value
vs
btw nice pun
if you have:
decleration
Code: Select all
int i;
int *pti= &i;
int &rti = i;
Code: Select all
pti++
assigning value
Code: Select all
*pti = 10;
Code: Select all
rti = 10;
hurtshifter wrote:So I have question which may sound a bit amateur
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- short
- ES Beta Backer
- Posts: 548
- Joined: Thu Apr 30, 2009 2:22 am
- Current Project: c++, c
- Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
- Programming Language of Choice: c, c++
- Location: Oregon, US
Re: & vs. *
lol, i think I was still hung over when I wrote that code this morning.
THanks
THanks
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
- hurstshifter
- ES Beta Backer
- Posts: 713
- Joined: Mon Jun 08, 2009 8:33 pm
- Favorite Gaming Platforms: SNES
- Programming Language of Choice: C/++
- Location: Boston, MA
- Contact:
Re: & vs. *
I see. So references are like a one woman man, while pointers don't mind jumping around from address to address.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
http://www.thenerdnight.com
- Netwatcher
- Chaos Rift Junior
- Posts: 378
- Joined: Sun Jun 07, 2009 2:49 am
- Current Project: The Awesome Game (Actual title)
- Favorite Gaming Platforms: Cabbage, Ground beef
- Programming Language of Choice: C++
- Location: Rehovot, Israel
Re: & vs. *
hurstshifter wrote:I see. So references are like a one woman man, while pointers don't mind jumping around from address to address.
hmm... more like you are the pimp and you tell the pointer what address he needs to be on
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: & vs. *
As vince the shamwow guy would say if he had a f****** clue about pointers and references!
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
if you don't have a clue what i'm going on about, explore these videos at your leisurewith 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!
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
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
- derbon
- Chaos Rift Cool Newbie
- Posts: 54
- Joined: Tue Jun 09, 2009 7:25 pm
- Current Project: Big Nasty Enemies
- Favorite Gaming Platforms: NES SMS snes PC N64 PS2 Vectrex The Arcade Machine
- Programming Language of Choice: C++, Perl
- Location: why shud i tell u u mite steal my tv
- Contact:
Re: & vs. *
what??
i think the best paint program would be microsoft paint + paint.NET+graphics gale + Paint shop Pro 7 + photoshop CS3, it would be called Paint Gale Pro Shop.NET,
http://youtube.com/gonduda
http://youtube.com/gonduda
Re: & vs. *
I love rap chop... "You're gonna love my nuts!"
BTW that was the best analogy ever....
BTW that was the best analogy ever....
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: & vs. *
References are implemented at compile time as auto dereferenced pointers. Thinking of them in that light has always helped me.
- Netwatcher
- Chaos Rift Junior
- Posts: 378
- Joined: Sun Jun 07, 2009 2:49 am
- Current Project: The Awesome Game (Actual title)
- Favorite Gaming Platforms: Cabbage, Ground beef
- Programming Language of Choice: C++
- Location: Rehovot, Israel
Re: & vs. *
OMG... Dr.seuss and the ShamWoW guy... ewwwprogrammerinprogress wrote:As vince the shamwow guy would say if he had a f****** clue about pointers and references!
BIM BAM POW, BUY THIS NOW!
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- hurstshifter
- ES Beta Backer
- Posts: 713
- Joined: Mon Jun 08, 2009 8:33 pm
- Favorite Gaming Platforms: SNES
- Programming Language of Choice: C/++
- Location: Boston, MA
- Contact:
Re: & vs. *
That definitely helpsGyroVorbis wrote:References are implemented at compile time as auto dereferenced pointers. Thinking of them in that light has always helped me.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
http://www.thenerdnight.com