Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Computer Science – XII

Chapter 1 – Structures and Pointers


Part 2 - Pointers

• Definition - Pointers are a derived data type that holds address of memory location
• Pointer is primitive since it contains memory address, which is atomic in nature
• Size of a pointer in C++ is usually 2 to 4 bytes
• Advantage: Can access memory locations faster by specifying memory addresses directly
• Declaring pointer variables: Ex. int *ptr;

Operators to use with pointers:

address-of operator (&) value-at operator (*) OR indirection / de-reference operator


Unary operator Unary operator
Used to get address of variable Used to get value pointed to by pointer
Can be used with any variable Can be used only with pointers
Ex. ptr = &num; Ex. cout << *ptr;

• Methods of memory allocation:


1. Static memory allocation - Memory allocation during compilation
2. Dynamic memory allocation - Memory allocation during execution
• Dynamic operators:
o new - unary operator used to allocate memory during run-time
o delete - used to deallocate memory before exiting the program
• Dynamic memory allocation: Using new operator
o Ex. int *ptr;
ptr = new int;
*ptr = 10;
o Initializing along with dynamic memory allocation: ptr = new int(5);
o Dynamically allocated memory location can be accessed only by indirection operator
• De-allocating memory: Using delete operator
Ex. delete ptr;

Static memory allocation Dynamic memory allocation


Takes place before execution of program Takes place during execution of program
Does not require any special operator Requires dynamic allocation operator new
Normal variables can be used Pointers must be used
Data is referenced using variables Data is referenced using indirection operator
No statement is needed for de-allocation Requires delete operator to release memory

• Memory leak:
o When pointer variables declared using new are not freed using delete, part of
memory seems to disappear on every program run.
o To avoid memory leak, memory allocated using new operator must be deallocated
using delete before exiting program.
o In case of static allocation, OS takes care of this; hence there will be no memory leak

Deepa T, HSST Computer Science 1


Computer Science – XII

• Operations on pointers
o Arithmetic operations - Increment / Decrement
▪ Returns memory address of next element of its data type
o Relational operations - Equality (==) and Non-equality (!=)
▪ Checks whether two pointers contain address of the same memory location

• Pointer and Array


o Array-name contains base address of the array; hence array names are pointers
pointing to first element of the array
o Adv of pointers over arrays - Using array causes wastage or insufficiency of memory
o To access array using pointers, store address of the first location of array in a
pointer. The pointer can be incremented to access each element
o Note: pointer_name++ is valid, but array_name++ is invalid
o Dynamic array – Array created at run time using new operator
▪ Ex. ptr = new int [5];

int *p = new int(10); int *p = new int[10];


Dynamic memory allocation, initialized Dynamic array of 10 elements is created
with value 10 using dynamic memory allocation

• Pointer and String


o A string is an array of characters; hence string is also a pointer
o Advantages of using character pointers:
1. Any number of characters can be stored without wastage or
insufficiency of memory space
2. Assignment operator can be used to copy strings
3. Faster access by using pointer arithmetic
4. Array of strings can be managed with optimal use of memory space
o Array of strings - char *name[3] = {"morning", "noon", "night"};
declares an array name that can contain 3 strings of any length

• Pointer and Structure


o We can declare a pointer to a structure as follows:
student *sptr;
sptr = new student;
o To access elements of the structure, use arrow operator: sptr -> admno

• Self-referential structure
o A structure in which one of the elements is a pointer to the same structure.
o Ex. Linked list

• Dynamic data structure


o Collection of data for which memory will be allocated during run time
o They have scattered memory locations but links from one location to the next
o Ex. Linked list

Deepa T, HSST Computer Science 2

You might also like