Hi, I was wondering what the difference between a pointer and a reference is. I generally references when passing classes or something with a structure.
Pointers/references
Started by RobotGymnast, Sep 18 2008 03:07 AM
31 replies to this topic
#1
Posted 18 September 2008 - 03:07 AM
|
|
|
#2
Posted 18 September 2008 - 03:26 AM
A pointer contains a memory address, which can be dereferenced to get access to the contents of the memory address it contains.
A reference is a second variable name for the same contents.
A pointer can have the memory address it contains changed, thus changing what it is referring to. A reference cannot change which variable it is a synonym for.
A pointer does not have to be initialized when it is declared. A reference has to be initialized when it is declared.
References are great in function calls where you want to be able to change a parameter, and they are slightly easier to work with than pointers (especially for people with an irrational fear of pointers).
A reference is a second variable name for the same contents.
A pointer can have the memory address it contains changed, thus changing what it is referring to. A reference cannot change which variable it is a synonym for.
A pointer does not have to be initialized when it is declared. A reference has to be initialized when it is declared.
References are great in function calls where you want to be able to change a parameter, and they are slightly easier to work with than pointers (especially for people with an irrational fear of pointers).
#3
Posted 18 September 2008 - 04:34 PM
okay, thanks. I get it mostly now. I tend to do more direct manipulation, so more pointer stuff xD. thanks
#4
Posted 18 September 2008 - 07:10 PM
My professor made me watch this ridiculous video. However, it does do a descent job on describing pointers: Binky Pointer Fun Video
#5
Posted 19 September 2008 - 10:59 AM
John said:
My professor made me watch this ridiculous video. However, it does do a descent job on describing pointers: Binky Pointer Fun Video
Thats a great video, kind of childish, but it does get the crystal clear 'pointer' across ;)
#6
Posted 19 September 2008 - 11:23 AM
I know it is a great video. that is why I posted it. :) but thanks
#7
Posted 23 September 2008 - 04:33 PM
lol.
"Because it does not refer to a meaningful object, an attempt to dereference a null pointer usually causes a run-time error that, if unhandled, terminates the program immediately. In the case of C, execution halts with a segmentation fault because the literal address of NULL is never allocated to a running program."
Yeah, don't dereference a NULL pointer. Bad things happen.
"Because it does not refer to a meaningful object, an attempt to dereference a null pointer usually causes a run-time error that, if unhandled, terminates the program immediately. In the case of C, execution halts with a segmentation fault because the literal address of NULL is never allocated to a running program."
Yeah, don't dereference a NULL pointer. Bad things happen.
#8
Posted 23 September 2008 - 05:52 PM
Yeah, you have to go like
sort of annoying.
if(pointer) delete pointer;
sort of annoying.
#9
Posted 23 September 2008 - 06:10 PM
John said:
My professor made me watch this ridiculous video. However, it does do a descent job on describing pointers: Binky Pointer Fun Video
I watched the Ada version of that in my CS3 class, it does the job, but is really silly.
#10
Posted 23 September 2008 - 06:13 PM
Unless I'm mistaken, and I very well could be, deleting a NULL pointer is a noop command, and won't do anything useful, since NULL in C isn't actually allocated memory space... Correct me please if I'm wrong, I'm not 100%.
#11
Posted 23 September 2008 - 07:20 PM
Yeah deleting a NULL pointer is pointless. It won't have any negative effects, but it's unnecessary.
#12
Posted 23 September 2008 - 09:54 PM
Well, actuuaaaaalllly. There's a lot of fun to be had with deleting NULL.
On older computers, where Ring protection and memory rights weren't available, it was possible to delete NULL. This would cause the computer to triple fault and die immediately :D
On older computers, where Ring protection and memory rights weren't available, it was possible to delete NULL. This would cause the computer to triple fault and die immediately :D


Sign In
Create Account


Back to top









