pointers, references and variables in any language

Posted on 2014-07-27
Last Modified: 2014-07-29
Please explain the diffenence between pointers, references and variables

This is a python example but the pointers, references and variables is in c++, so I can not test

Is there a way I can test.
For example makes it easy to type in code into a text editor
Question by:rgb192
LVL 26

Accepted Solution

dpearson earned 167 total points
ID: 40223314
In all languages a variable stores a value.  The value can be a number, a string, a floating point value etc.

The value inside a variable can also be a memory location.  When it is, then we call that either a pointer or a reference.

A pointer always means a variable that stores an explicit memory location.  Usually the language allows that pointer to be changed, to point to any other memory location you choose - including completely invalid ones.  (Languages like C++ allow this).

The meaning of a 'reference' can vary a little between languages.  But generally, if the memory location is required by the language to be the address of another object, then we call that a reference.  (Languages like Java only allow references - you can't create a pointer to anything except another object).

So we have:
 a) Variable - something which can store a value
 b) Pointer - a value that is a specific memory location (the location is 'pointed' to by the variable)
 c) Reference - a value that is the memory location of another object in the language (i.e. more specific than a pointer which can point to arbitrary places in memory - not just other objects).

Hope that helps,

LVL 28

Assisted Solution

pepr earned 167 total points
ID: 40223633
+1 to dpearson explanation. To add for reference. To understand it, you should think in terms reference value and reference variable. Similarly, you can think in terms pointer value and pointer variable. The reference value and the pointer value are exactly the same and it is the memory address of the target. The pointer variable is related to the space where the pointer value is stored, the reference variable is related to the space where the reference value is stored. As the values are memory addresses in both types, both variables (pointer and reference) need the same memory space to store its value -- 4 bytes in a 32-bit operating system, or 8 bytes in a 64-bit operating system).

The main difference between pointers and references (in C++) is that for indirect access to the target through the pointer variable, the pointer must be dereferenced explicitly using the *ptr when accessing the pointed target as a whole, or using the ptr->part when accessing the part of the referenced target.

For indirect access through the reference variable (say named ref that stores the address), using ref means automatic dereferencing. Logically and syntactically it is exactly the same as with working with the target directly. So, when accessing the part of the referenced target, you write ref.part.

Technically, both pointers and references has the same implementation -- indirect access through the stored address. Working with pointers is more explicit. There also is a special constant nullptr (or NULL in earlier standards and also in the C language) that can be assigned to the ptr variable with meaning pointing to nowhere. The reference variable is always required to point to a valid target (i.e. more restrictive) so that it could always be used syntactically as if working with the target directly.

In C++ and the other compiled languages, variable names are converted to addresses. There is no need to keep the string name in the binary program. A lot of variables is accessed directly. Only when you explicitly say, the target can be allocated dynamically (the exact address known only in runtime) and reference via pointers or references. In Java, every object is created dynamically and access via references (i.e more restrictive rules in comparison to pointers and therefore simpler to be used by the programmer). In Python, every object is created dynamically the similar way. The reference variables are always placed in one of the internal Python dictionaries where the string name of the variable is the key, and reference value (the address of the target) is the value of the dictionary item.

Author Comment

ID: 40224699
so reference is storage space of a variable value

and pointer is storage space of a reference value?
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

LVL 28

Expert Comment

ID: 40224953
Reference in the abstract sense is the value that refers to the target object (where the object can be as simple as an integer variable). The concrete implementation of a reference is the address of the target stored in another variable (speaking about compiled languages as C++, for example). If that dereferencing is done automatically, then it looks as if the extra variable did not exist and as if one worked directly with the target object. But it is only the illusion -- the access is indirect. If the dereferencing is done explicitly, then we call it a pointer.

When speaking about a pointer, we can think about both the pointer variable and the address of the target stored inside. We can think this way also when thinking about a reference. However, a reference usually mimics the target object so well that some people tend to think about it (incorrectly) as about the target object itself.
LVL 33

Assisted Solution

sarabande earned 166 total points
ID: 40226123
I would like to add some samples o the great explanations given above:

(A) by value

void f(string s) 
      s += " World";
      cout << s; 

string str = "Hello";
cout << str;  // prints "Hello"

Open in new window

the string str was passed as a copy to the function f. the compiler allocates a new memory space on the stack with size of string and then the copy constructor of string was called to create a copy. whatever f does with the variable s, the caller's str would not change its contents. when the control returned from f to the caller the copy s was deleted and the storage was freed.

(B) by pointer

void f(string * s) 
      if (s != NULL)
             *s += " World";
             cout << s->c_str() << endl;
             s = new string("some other text");
             cout << s->c_str() << endl;

string str = "Hello";
cout << str;  // prints "Hello World"

Open in new window

the string str was passed by pointer (address) to the function f. the compiler allocates a new memory space on the stack with size of pointer and then the address of string str was copied to the new pointer variable.  if pointer s was dereferenced in f and the string where s is pointing to was changed then the caller's str also would change as the pointer still points to the original storage. but if a new address was assigned to the pointer variable, it doesn't have any impact to the original string.

(B) by reference

void f(string & s) 
     s += " World";
     cout << s.c_str() << endl;
     s = "Another World";

string str = "Hello";
cout << str;  // prints "Another World"

Open in new window

the string str was passed by reference (address) to the function f. the compiler allocates a new memory space on the stack with size of pointer and then the address of string str was copied to the storage.  the string s in f is an alias of the caller's str. if it was changed in f the caller's str also would change. the stack storage is same as when passing by pointer but no pointer variable was created. the stack storage only serves to pass the address of the original variable,


Author Closing Comment

ID: 40228138
thanks for all advice.
I am starting to understand.

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question