Professional Documents
Culture Documents
Chapter 1 - Structures and Pointers
Chapter 1 - Structures and 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;
• 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
• 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
• Self-referential structure
o A structure in which one of the elements is a pointer to the same structure.
o Ex. Linked list