POINTERS / DYNAMIC MEMORY Lecture 10
ALLOCATION Section-I
Prepared by: Mian Saeed Akbar
POINTERS
Pointer
o Pointers are variables that store memory addresses
o They allow direct manipulation of memory, which is one of the powerful features
of the language
o Pointers can be used to access and modify data, to pass data by reference, and
to create dynamic data structures
o A pointer must be defined before it can be used
o It should be initialized to NULL or valid address
POINTERS
Declaring Pointers
o Pointers are declared using the * symbol before the variable name
o For example, to declares a pointer to an integer
int* ptr; or int* ptr = NULL;
NULL Pointer
o A NULL pointer is a pointer that doesn't point to any valid memory address
o It is usually assigned when a pointer is declared but not yet assigned a valid
memory address
o A NULL pointer can lead to program crashes if it's dereferenced without being
assigned a valid memory address first
POINTERS
Assignment
o Pointers are assigned memory addresses using the & operator
o For example, to assigns the memory address of x to the pointer ptr
int x = 5;
int* ptr = &x;
o The variable must exists before its address assignment
POINTERS
Pointer Operators
o Pointer operators are used before the variable name
o These are * and & operators
* operator
Indirection operator or dereferencing operator
Returns a synonym, alias or nickname to which its operand points
& operator
Address of operator
Returns the address of its operand
POINTERS
Dereferencing
o A pointer variable has associated two values
1. A direct value, that is the address of a variable it is pointing. It can be
referenced by using the pointer name
2. And Indirect value, the value of the variable to which the pointer is pointing
o Dereferencing a pointer means accessing the value of the variable to which the
pointer is pointing (indirect value)
o The dereference operator * is used for this
o For example,
int x = 5;
int* ptr = &x;
int y = *ptr; // assign the value of x to y
POINTERS
Pointer Arithmetic
o Pointers can be incremented or decremented using arithmetic operations such as
++, --, +, -
o This allows traversal through arrays and dynamic data structures
o Pointer arithmetic works by using the size of the data type being pointed to
o When a pointer is incremented or decremented, it moves to the next or previous
memory location based on the size of the data type
o For example, if you have a integer pointer intPtr, incrementing it by 1 would
move it to the next memory location that can hold an integer value
o Since a integer variable typically takes 4 bytes, the pointer would move 4 bytes
forward
POINTERS
o Pointer arithmetic is not limited to just incrementing or decrementing by 1
o You can also add or subtract a pointer by a certain number (integer), which will
move it forward or backward by that many times the size of the data type
o One thing to note is that pointer arithmetic should only be performed on pointers
that are pointing to elements of an array or one element past the end of an
array
o If pointer arithmetic is done on a pointer that is not pointing to an array, the
behavior is undefined and can lead to errors or unpredictable results
POINTERS
Pointers and Arrays
o Arrays and pointers are closely related
o The name of an array is a pointer constant to the first element of the array
o When an array is accessed using an index, it is equivalent to dereferencing a
pointer to the array's first element
o For example,
int scores[10] = {87,98,93,87,83,76,86,83,86,77};
int *scorePtr = NULL;
scorePtr = scores;
POINTERS
o Element of the array at index n can also be accessed using the pointer (first
element) + integer n i.e
scores[5] is same as *(scoresPtr+5)
POINTERS
Task
Write a program
DYNAMIC MEMORY Lecture 10
ALLOCATION Section-II
Prepared by: Mian Saeed Akbar
DYNAMIC MEMORY ALLOCATION
Dynamic Allocation of Memory
o So far, we have always allocated memory for variables that are located on the
stack
o The size of such variables must be known at compile time
o Sometimes convenient to allocate memory at run time
o Dynamic memory allocation is the process of allocating and managing memory at
runtime
o It allows the program to request memory from the operating system dynamically,
rather than relying solely on the fixed amount of memory that is allocated at
compile time
o System maintains a second storage area called the heap
DYNAMIC MEMORY ALLOCATION
o Dynamic memory allocation is primarily done using the following functions
malloc()
It is used to allocate a block of memory of a specified size in bytes
It returns a void pointer (i.e., a generic pointer) which can be cast to the
desired data type
It takes one argument – the total size of memory block to be allocated
If the allocation fails, it returns NULL
calloc()
It is similar to malloc(), but it initializes the allocated memory to zero
It takes two arguments – the number of elements to be allocated and the
size of each element
It returns a void pointer.
DYNAMIC MEMORY ALLOCATION
realloc()
It is used to change the size of the previously allocated memory block
It takes two arguments - a pointer to the previously allocated memory
block and the new size
It returns a void pointer which may or may not be the same as the
original pointer
free()
It is used to deallocate the previously allocated memory block
It takes a pointer to the memory block as an argument and frees the
allocated memory
DYNAMIC MEMORY ALLOCATION
o Example
Thank you