sábado, 30 de noviembre de 2013

Pointers


About pointers...
  • A pointer is the memory address of a variable. 
  • Memory addresses can be used as names for variables.  
    • If a variable is stored in three memory locations, the address of the first can be used as a name for the variable.
    • When a variable is used as a call-by-reference argument, its address is passed.
  • An address used to tell where a variable is stored in memory is a pointer.
Declaring Pointers

Pointer variables must be declared to have a pointer type.  For example,to declare a pointer variable p that can "point" to a variable of type double:

double *p;
  • The asterisks identifies p as a pointer variable


To declare multiple pointers in a statement, use the asterisk before each pointer variable.  For example:

int *p1, *p2, v1, v2;

Operators with Pointers:

The & operator can be used to determine the address of a variable which can be assigned to a pointer variable.  For example:  

p1 = &v1;

p1 is now a pointer to v1
v1 can be called v1 or "the variable pointed to by p1"

C++ uses the * operator in yet another way with pointers.

A Pointer example:

v1 = 0;
p1 = &v1;
*p1 = 42;
cout << v1 << endl;
cout << *p1 << endl;

Output:
42
42

The assignment operator = is used to assign the value of one pointer to another.  For example, if p1 still points to v1 then:

p2 = p1;

causes *p2, *p1, and v1 all to name the same variable.



The new Operator

Using pointers, variables can be manipulated even if there is no identifier for them.  To create a pointer to a new "nameless" variable of type int we use:  p1 = new int; 

The new variable is referred to as *p1  

Variables created using the new operator are called dynamic variables.  For example:


Explanation:


new and Class Types

Using operator new with class types calls a constructor as well as allocating memory.  If MyType is a class type, then:
  • MyType *myPtr;                              // creates a pointer to a variable of type MyType
  • myPtr = new MyType;                     // calls the default constructor
  • myPtr = new MyType (32.0, 17);    // calls Mytype(double, int);
New dynamic variables use memory in the freestore.  If all of the freestore is used, calls to new will
fail.  When variables are no longer needed, they can be deleted and the memory they used is returned to the freestore.

When dynamic variables are no longer needed, delete them to return memory to the freestore.  For example:

delete p;

The value of p is now undefined and the memory used by the variable that p pointed to is back in the freestore.

Dangling Pointers:
  • Using delete on a pointer variable destroys the dynamic variable pointed to.
  • If another pointer variable was pointing to the dynamic variable, that variable is also undefined.
  • Undefined pointer variables are called dangling pointers.
    • Dereferencing a dangling pointer (*p) is usually disastrous.
Automatic Variables:

Variables declared in a function are created by C++ and destroyed when the function ends.  These are called automatic variables because their creation and destruction is controlled automatically.  The programmer manually controls creation and destruction of pointer variables with operators new and delete

Variables declared outside any function definition are global variables.

Type Definitions:

A name can be assigned to a type definition, then used to declare variables.  The keyword typedef is used to define new type names. 

Syntax: typedef Known_Type_Definition New_Type_Name;

To avoid mistakes using pointers, define a pointer type name.  For example:

typedef int* IntPtr;

Defines a new type, IntPtr, for pointer variables containing pointers to int variables.

IntPtr p;    is equivalent to    int *p;

A second advantage in using typedef to define a pointer type is seen in parameter lists.  For example:

void sample_function(IntPtr& pointer_var);

is less confusing than

void sample_function( int*& pointer_var);



No hay comentarios.:

Publicar un comentario