Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


pointers, references and variables in any language

Posted on 2014-07-27
Medium Priority
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 w3schools.com makes it easy to type in code into a text editor
Question by:rgb192
LVL 28

Accepted Solution

dpearson earned 668 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 29

Assisted Solution

pepr earned 668 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?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 29

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 35

Assisted Solution

sarabande earned 664 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

577 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