Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers vs. References in C++ function arguments [duplicate]

Tags:

c++

pointers

I was wondering how to make a function alter two variables (the return and another one) and I stumbled upon calling the function with an '&' before the parameter (which I understand to mean the address of the parameter) then throughout your function, referencing it with the '*' sign (which I guess is a "dereference" and means it alters the object at the address).

Anyways, this was all going fine, then a friend said you can just call the function with the variable directly, refer to the variable with an & before it in the header, and treat it normally throughout the function. This seems way easier, so why isn't there more about it on the web? Is one style more correct than the other?

void foo(int &junk)  //The way the friend said
{
    junk++;
}

void oof(int *junk) //what I found, and what the internet seems full of
{
    (*junk)++;
}

int main ()
{
    int junk=1;
    std::cout << junk << "\n";
    foo(junk);
    std::cout << junk << "\n";
    oof(&junk);
    std::cout << junk;
}

This outputs:

1
2
3

So everything works fine, I'd assume.

like image 294
Jackson Avatar asked Jul 27 '13 16:07

Jackson


People also ask

Is it better to use pointer or reference?

References are usually preferred over pointers whenever you don't need “reseating”. This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.

Are references more efficient than pointers?

Yes, references can be more efficient than pointers. The reason lies in optimizers. In general, one of the most expensive operations in a modern CPU is a load from memory. And after you've called a (non-inlined) function, the compiler must assume that any variable in memory that could change, did change.

What is the difference between passing an argument by pointer and by reference?

So, if we pass parameter to a function either by pass by pointer or pass by reference it will produce the same result. Only difference is that References are used to refer an existing variable in another name whereas pointers are used to store address of variable.

What is faster reference or pointer?

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.


1 Answers

The you first function foo, you are passing by reference :

When a variable is passed by reference we are not passing a copy of its value, but we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their counterpart variables passed as arguments in the call to the function.

An example of passing by reference :

Passing by reference

In your second example oof, you are passing a pointer to the variable.

If you want to know the different between both example, I suggest you to read this : https://stackoverflow.com/a/57492/1394283

But, When you should use references and when you should use pointer ?

I will say use references whenever you can, use pointers whenever you must.

The reason is that pointers makes things harder to follow/read, less safe and far more dangerous manipulations than any other constructs.

This post explains it very well : https://stackoverflow.com/a/7058373/1394283

like image 151
Pierre Fourgeaud Avatar answered Sep 22 '22 03:09

Pierre Fourgeaud