Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference vs. pointer

What is the difference? Because this:

int Value = 50;
int *pValue = &Value;

*pValue = 88;

and ref version do the same:

int Value = 50;
int &rValue = Value;

rValue = 88;

Which one is better to use? Thanks.

like image 648
Quest Avatar asked Feb 19 '14 15:02

Quest


People also ask

What is the difference between reference and pointer?

References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can. A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.

Are reference faster than pointers?

It's much faster and memory-efficient to copy a pointer than to copy many of the things a pointer is likely to point to. A reference is stored in as many bytes as required to hold an address on the computer. This often makes reference much smaller than the things they refer to.

Should I pass by reference or pointer?

The difference between pass-by-reference and pass-by-pointer is that pointers can be NULL or reassigned whereas references cannot. Use pass-by-pointer if NULL is a valid parameter value or if you want to reassign the pointer. Otherwise, use constant or non-constant references to pass arguments.

Are references just pointers?

Differences between pointers and references in C++ A pointer in C++ is a variable that holds the memory address of another variable. A reference is an alias for an already existing variable. Once a reference is initialized to a variable, it cannot be changed to refer to another variable.


2 Answers

In this case, they are equivalent.

It does not matter which you use, and neither is "best".

If you really want to choose between them then the reference is probably more idiomatic. I generally stick to references wherever I can because my OCD likes it: they feel "tighter", cannot be re-bound (with or without you noticing) and don't require a dereference to get to the value.

But I'm not aware of any general consensus on the issue for cases such as this.

Also note that the two may not compile to the same code if your implementation does not implement references with pointers, though I know of no implementation like that, and you wouldn't notice the difference anyway.

like image 77
Lightness Races in Orbit Avatar answered Sep 23 '22 15:09

Lightness Races in Orbit


A pointer is the address of the memory location. You can change the value of that address to point at different memory addresses.

A reference is an alias of the variable. You can only assign this alias during declaration. You cannot change which variable the reference is an alias of after it's declared.


The following pointer assignments are not possible with references.

int a = 10;
int b = 20;

int* pInt = NULL; // A pointer pointing at nothing.
pInt = &a; // pInt now points at a
pInt = &b; // pInt now points at b

As for which one is better, it all depends on context.

I use references for method and function parameters.

void updateFoo(Foo& foo)

I use references to alias complex objects.

Foo& foo = bar.getBaz().getFoo(); // easy access to foo

I use pointers for dynamically allocated objects.

Foo* pFoo = new Foo();

I use pointers for things which may point at different values (including no value at all).

Foo* pFoo = NULL;

if (condition1)
    pFoo = &foo1;
else (condition2)
    pFoo = &foo2;

As a general rule, I default to references and use pointers in places where the limitations on references cause problems.

like image 22
Jeffery Thomas Avatar answered Sep 21 '22 15:09

Jeffery Thomas