Pointers Vs References

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
independentCreations
Posts: 24
Joined: Fri Oct 28, 2005 10:05 am
Location: Gold Coast, QLD, Australia

Pointers Vs References

Post by independentCreations »

Is either one better.
pointers vs references, i have a mate that uses references rather than pointers, but i use pointers. Any one better than the other? Performance wise? Which one do you use?

Lets Here It :P
"do it, do it now"
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

What's the difference !?!?!
I always thought, pointers and referenzes are the same (like functions and methods)...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
pfo
Posts: 370
Joined: Mon Aug 29, 2005 10:54 pm
Location: http://web.utk.edu/~pfox1

Post by pfo »

They are the same thing, although they tend to apply to different languages. Typically, C or C++ uses the term pointers. Things like VB use the term reference, but it's really the same thing. VB references may be a little different too; I don't think they just simply store a 32-bit memory value, there may be some extra metadata
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

okay i'm a c programmer who's scared of references so I just spent ten whole minutes researching this. this should answer everything- http://www.parashift.com/c++-faq-lite/references.html
FlyHigh
Posts: 111
Joined: Wed Mar 23, 2005 12:05 am

Post by FlyHigh »

They are definatly not the same!

A reference is exactly that, an alias for an existing variable... Passing by reference (eg. void Funct(int &x); ) will make the x alias the integer that the calling function passed in meaning the function can change x's value and the change will be visible to the calling function.

A pointer is an address in memory that gets passed, which means you can do all the things mentioned previously and also do pointer arithmetic on it, and also move the pointer to point else where.


Also with pointers you can pass NULL (or 0) as well to signifiy another condition. Whereas with references you can't reference an NULL object.

In Irrlicht the clipping rectangles are always passed by pointer so if you specify null there is no clipping but if you specify a pointer to something valid than it will clip against that.

Whereas coordinates are always passed by reference because the function doesn't need to repoint the object or do pointer arithmetic or anything, it just needs the data from the object.

eg.

char GetSecondLetter(const char *txt) { return txt[1]; }
//would not be possible with references.

void Add5(int &x) { x += 5; } //works
void Add5(int x) { x += 5; } //doesn't work, value passed only

Basically, only pass by pointer if nessacary because the syntax is slightly less cleaner. (references and pointers create the same assembly code anyway so neithers faster than each other).
Guest

Post by Guest »

I think you mean the difference of:

Code: Select all

void increase_r(float &y)
{
  y += 1;
}

// -and-

void increase_p(float *y)
{
  *y += 1;
}
Both are virtually the same to the system as far as performance. I personally believe references are easier but it is really a matter of preference. There is less typing involved but that is the extent of it, you don't have to tell C to send the pointer to your variable and then in the function you don't have to tell C to use the value. References are basically refering to your parameter directly from the function that called it. A pointer is usually (32-bit at least) smaller than one float which means it is faster to pass.

Code: Select all

float height = 0;

increase_r(height);
increase_p(&height);
// height == 2
Best opportunity to use references is if you plan to pass a large structure, such as a matrix of four floats. When you pass it as a parameter, it is pushing all those numbers onto the stack and pulling them off after. If the function isn't going to change the values, why not just give it the memory that you are already using? Please note that arrays are automatically passed as a reference in most implementations of C++. That means if you change anything in your array in your function, it will be changed in the caller function too.
namik
Posts: 11
Joined: Thu Nov 03, 2005 8:12 pm
Contact:

Post by namik »

Forgot to log in...
needforhint
Posts: 322
Joined: Tue Aug 30, 2005 10:34 am
Location: slovakia

Post by needforhint »

"pointer" a variable that stores a memory address in itself
"reference" is a memory address itself

am I right?
what is this thing...
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

no, references are just pointers that restrict how you code. they're like pointers little brother or something
independentCreations
Posts: 24
Joined: Fri Oct 28, 2005 10:05 am
Location: Gold Coast, QLD, Australia

Post by independentCreations »

i win - k so i beat you Twinsen, references are const that y i prefer to use pointers. And because of pointers and the way they are implemented you can forget about the actual object for the mean time, except when realising memory, but ah there you go
Last edited by independentCreations on Sun Nov 06, 2005 1:35 pm, edited 1 time in total.
"do it, do it now"
Twinsen
Posts: 27
Joined: Thu Nov 03, 2005 2:05 pm
Location: Gold Coast

Post by Twinsen »

LOL, references r indeed an alias to a thing
pointers store the memory address to the thing.
So in a sense they both r the same and do the same job, but i beleive that pointers r good 4 objects and references 4 passn smaller things.
Also the major difference is that fact that references cant be chanegd, where as pointers can, so depending on wat you want to do, you can make a decision.
"Sorry, Twinsen, you don't have enough Kashes. "- Yeah right pal
zenaku
Posts: 212
Joined: Tue Jun 07, 2005 11:23 pm

Post by zenaku »

bitplane wrote:no, references are just pointers that restrict how you code. they're like pointers little brother or something
:lol:

That's actually a pretty accurate statement. They were designed to make pointers a little more safe to use. References cannot reference NULL, for example, but pointers can point to it. Dereferencing a NULL pointer is probably the most common C programming bug.

Internally they are both just addresses. In terms of performance, neither one is any better than the other. There might be cases where an optimizing compiler would compile a reference away completely, but leave a pointer variable around, and vise-versa, but for the most part it's insignificant.

If you are programming C++, it's simply better practice to use references than pointers when you can. In C, you didn't have the option (not counting C-99 stuff ;) ).
Post Reply