What Are the Differences Between a Pointer Variable and a Reference Variable

What is the difference between a variable and a reference in C++? [duplicate]

Here is a way to understand the difference:

  • Objects that can change state are also called "variables".
  • Pointers are objects (variable or not). They have a state.
  • References are "nicknames". They don't have a state, but expose the state of the refereed object (which is why you can't re-assign a reference, it's not an actual object).

Now, in some cases references might be implemented as pointers, but from the language point of view, references are just not pointers, they really are additional names for an object already existing.

As pointers are objects, and have a state, passing pointers to functions will copy that state, the pointer's state, not the pointee's state. However, references have no state, so if you pass a reference to a function, it's the refereed object that you pass (by copy).

By observation, the use of variables names and references is
exchangeable. Is that true?

"References are nickname" is the best way to understand references.

What is the difference between passing a variable name as parameter
and passing a reference? e.g.,

void func(int a); vs void func2(int& b);

The first implementation ask for a copy of the object passed. That is, internally func() can do anything to a, without changing the object that was passed to func() because internally func() made a copy of that object and manipulates the copy, not the original.

The second implementation ask for "a nickname for an object already existing". First, the object have to exist and if passed, a nickname for it will be created inside the function. That nickname, the reference b, is still a nickname for the original object. This mean that any manipulation done to b will affect the original object passed to func2().

  • func() signature says "I need this data but I will not modify the original object passed.".
  • func2() signature says "I need an object that I WILL certainly modify, pass it so that I can modify it".

Bonus stage:

At this point, if you don't know yet about const, that might be useful: in function signatures const is used with references to specify the arguments that are "read-only".

Let me clarify:

void func3( const int& b);

Here func3 says: "I need to access to an object, but really I will not make a copy of it. However I guarantee that I will not change that object".

So, why would we need that? Because some objects are expensive to copy. int is cheap to copy so most people will just pass it and func() and func3() are basically equivalent (depends on implementation but generally true).

If however we want to pass, says, a very big object, like a data buffer, we really don't want to copy it again and again just to apply some algorithms.
So we do want to pass it by reference. However, depending on the function, sometime you want to extract information and work with it, so you only need "read-only" access to the argument. In this case you use const Object&. However, if you need to apply the algorithm to the object passed, you need to be able to modify it, which you could call "write-access". In this case, you need to use a normal reference.
Asking for a copy basically mean that you want to manipulate an object that is the same state than the passed object, but is not the passed object.

To summarize:

  • func( T object ) : I want to have a copy of an object of type T;
  • func( T& object ) : I want to have "write-access" to an object of type T - assume that I will modify that object!;
  • func( const T& object ) or func( T const & object ) // which are the same : I want to read the state of an object, but I guarantee you that I will not modify it, I want "read-only" access.

Actually, the "read-only" guarantee could be violated using const_cast<> but that's a different story and it's only used in some very very very narrow cases.

Last thing you need to know is that if you have a member function, then you can do:

class K{
public:
void func() const; // see the const?
};

In this specific case, what you say is that inside the function, which is basically equivalent to:
void func( const K* this );

In this case you can see that this is a pointer but it's pointing to a const object. This mean that func() guarantee that the object it is member of (this) is never modified through this function (except some specific cases, see mutable keyword, another long story).

What is the real difference between Pointers and References?

It's all just indirection: The ability to not deal with data, but say "I'll direct you to some data, over there". You have the same concept in Java and C#, but only in reference format.

The key differences are that references are effectively immutable signposts - they always point to something. This is useful, and easy to understand, but less flexible than the C pointer model. C pointers are signposts that you can happily rewrite. You know that the string you're looking for is next door to the string being pointed at? Well, just slightly alter the signpost.

This couples well with C's "close to the bone, low level knowledge required" approach. We know that a char* foo consists of a set of characters beginning at the location pointed to by the foo signpost. If we also know that the string is at least 10 characters long, we can change the signpost to (foo + 5) to point at then same string, but start half the length in.

This flexibility is useful when you know what you're doing, and death if you don't (where "know" is more than just "know the language", it's "know the exact state of the program"). Get it wrong, and your signpost is directing you off the edge of a cliff. References don't let you fiddle, so you're much more confident that you can follow them without risk (especially when coupled with rules like "A referenced object will never disappear", as in most Garbage collected languages).

What is the difference between a pointer and a reference variable in Java?

A reference is sort of like a pointer that you can't do arithmetic on... although it's more opaque. While the underlying bits may be an address in virtual memory, they don't have to be. They're just a way of getting to an object (or representing the null value). So while they're not exactly the same, if you're used to thinking of a pointer as "a way of identifying an object or navigating to it" (in some sense) then yes, those thoughts apply to references too.

Java doesn't have pointers as such (unlike, say, C# which has references and pointers - the latter being used in "unsafe" code).

Difference between declaring variables like &ref=a and ref=&a in C++

&ref=a : It is the reference variable means it's is the another name for existing variable. It can be used like normal variables.Memory management of references left up to compiler

*ref=&a : It is the pointer variable it holds the address of another variable. Pointers are used to store and manage the addresses of dynamically allocated blocks of memory.

And the my answer is YES, Both should serves the same purpose in holding variable because in most compilers the reference can thought as constant pointer itself.

What is the difference between a C# Reference and a Pointer?

C# references can, and will be relocated by garbage collector but normal pointers are static. This is why we use fixed keyword when acquiring a pointer to an array element, to prevent it from getting moved.

EDIT: Conceptually, yes. They are more or less the same.



Related Topics



Leave a reply



Submit