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 27

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 29

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?
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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 34

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where are the c++ header files to use in Eclipse? 4 52
Java syntax, or is it Selenium 6 42
learn programming 8 70
Where to place postgres JDBC driver jar on tomcat 8 31
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

733 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