You are on page 1of 14

Pointers

COMPRO2

Neil Bretana

Review
Stackbased memory The stack is a data structure that allows insertion and deletion of data to occur only at one end, the top of the stack. Every program uses a stack to manage its storage requirements during execution. The stack is used for storing variables in the program.

Review
Heapbased memory In some cases, a request for additional memory locations on demand and a need to release them when they are no longer needed are required in a program. This calls the need for using the heap-based memory. The heap-based memory is used to store dynamic variables.

What are Pointers?


Pointers are variables that points to locations in the heap memory
Note: The direct value of a pointer can only be an address. Imagine a treasure chest that contains only a map to the real treasure chest which contains the gold; The treasure chest containing the map serves the same purpose as a pointer

Declaring Pointers
The dereference (*) operator is used to declare a pointer variable Syntax: <data_type> *<pointer_variable_name>; Example: int *pInt; float *pFloat; char *pChar;
Note: Pointer variables can only point to or refer to the corresponding data type it was declared with. In this example, pInt can only point to integer values as pFloat and pChar can only point to float and char values, respectively.

De-allocating Dynamic Variables


stack heap

01223
pChar at address 07770 (a) stack

A
pChar at address 01223

A pointer is a variable used only to store the address of another memory space. In Figure a, pointer pChar at address 07770 contains the address of a dynamic character variable at address 01223 containing the value of A. For convenience, pointers can also be visualized as in Figure b. Given, the following figures, we can say that pChar points to the dynamic variable with a value A

heap

A
pChar at address 07770 (b) pChar at address 01223

Declaring Pointers
Before a pointer can be used for dynamic variables, we need to allocate it with enough memory space from the system heap. Syntax: The function call to malloc allocates <size> bytes of heap storage for the malloc(<size>) pointer. Example: int *pInt; pInt = malloc(sizeof(int));
Important: The <size > is given by the syntax sizeof(data_type)

Accessing Pointers
main() { int nNum; int *pNum; pNum = malloc(sizeof(int)); *pNum=10; }
nNum pNum Notice the difference in accessing the address stored in the pointer variable and the value that the pointer variable points to.

Stack Memory

Heap Memory

10

Accessing Pointers
int *pNum; //line 1 pNum = malloc(sizeof(int)); //line 2 *pNum=10; //line 3
stack heap

//after line 1

pNum heap

stack

//after line 2

pNum

stack

heap

//after line 3

pNum

10

De-allocating Dynamic Variables


In order to free or make a dynamic memory location available once again, we need to destroy or deallocate the dynamic variable stored in pointers. Syntax: free(<pointer_variable_name>) Example: int *pInt; pInt = malloc(sizeof(int)); *pInt = 71; free (pInt);
The function call to free will release/destroy the dynamic variable pointed to by pInt. Such variable now contains a garbage value unless we set it to NULL.

De-allocating Dynamic Variables


*pInt = 71; free(pInt); pInt = NULL; //line 1 //line 2 //line3
stack heap

//after line 1

pInt stack

71
heap

//after line 2

pInt Free for re-allocation heap

contains garbage stack

//after line 3

pInt Free for re-allocation

contains NULL

Retrieving an address
The address-of (&) operator is used to retrieve the memory location of a variable Syntax: &<variable_name>; Example: int *pInt; int vInt=21; pInt=&vInt;

Note: Passing the address of a variable to a pointer allows the pointer to point to the value of the variable.

Retrieving an address
int *pInt //line 1 Int vInt=21; //line 2 pInt = &vInt; //line3
stack heap

//after line 1

pInt stack heap

//after line 2

vInt pInt

21

stack

heap

//after line 3

vInt pInt

21

Recap
Dereference operator (*)
used to declare pointers

malloc(<size>)
used to allocate enough heap-based memory for dynamic variables

free(<pointer_variable_name>)
used to de-allocate heap memory by destroying dynamic variables

Address-of operator (&)


used to retrieve the address of a variable

You might also like