Professional Documents
Culture Documents
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.
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.
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
//after line 1
pInt stack
71
heap
//after line 2
//after line 3
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
//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