Solved

pointers, references and variables in any language

Posted on 2014-07-27
7
200 Views
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
http://www.experts-exchange.com/Programming/Languages/Scripting/Python/A_6589-Python-basics-illustrated-part-2.html

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

Accepted Solution

by:
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,

Doug
0
 
LVL 28

Assisted Solution

by:pepr
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.
0
 

Author Comment

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

and pointer is storage space of a reference value?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 28

Expert Comment

by:pepr
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.
0
 
LVL 32

Assisted Solution

by:sarabande
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";
f(str);
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";
f(&str);
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";
f(str);
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,

Sara
0
 

Author Closing Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now