0% found this document useful (0 votes)
0 views4 pages

CLass Note3

The document provides an overview of pointers in C and C++, explaining their definition, usage, and syntax. It covers various types of pointers, dynamic memory allocation, pointer arithmetic, and best practices to avoid common issues such as dangling pointers and memory leaks. Additionally, it highlights the advantages of using smart pointers in modern C++ for automatic memory management.

Uploaded by

greenbeltbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

CLass Note3

The document provides an overview of pointers in C and C++, explaining their definition, usage, and syntax. It covers various types of pointers, dynamic memory allocation, pointer arithmetic, and best practices to avoid common issues such as dangling pointers and memory leaks. Additionally, it highlights the advantages of using smart pointers in modern C++ for automatic memory management.

Uploaded by

greenbeltbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lecture Notes: Pointers in C and C++

Page 1: Introduction to Pointers


What is a Pointer?
A pointer is a variable that stores the memory address of another variable. Instead of holding a
value directly, a pointer holds the address of a variable that stores a value.

Why Use Pointers?


• Efficiency: Direct memory access can speed up programs.
• Dynamic Memory Allocation: Allows for flexible memory management.
• Pass-by-reference: Pointers are essential for passing large structures or arrays to functions
without copying data.
• Memory Management: Helps with manual memory control in languages like C and C++.

Pointer Syntax (in C and C++)


c
CopyEdit
int a = 5;
int *p; // Declare a pointer to an integer
p = &a; // Store the address of 'a' in pointer 'p'

Memory Address and Dereferencing


• &: Address-of operator.

• *: Dereference operator, used to access the value stored at the address the pointer is pointing
to.
c
CopyEdit
printf("Address of a: %p\n", &a); // Prints memory address of 'a'
printf("Value pointed to by p: %d\n", *p); // Dereferencing

Page 2: Types of Pointers


Null Pointer
A pointer that does not point to any valid memory address.
c
CopyEdit
int *ptr = NULL; // Initialize a pointer to NULL

• It's a good practice to initialize pointers to NULL to avoid them pointing to garbage values.
Void Pointer
A void* can hold the address of any data type, but cannot be dereferenced directly without
casting.
c
CopyEdit
void *ptr;
int num = 10;
ptr = # // void pointer can store any address

Pointer to Pointer
A pointer that points to another pointer.
c
CopyEdit
int **ptr2ptr;
int *ptr;
int a = 10;
ptr = &a;
ptr2ptr = &ptr; // ptr2ptr points to ptr, which points to a

Function Pointer
A pointer that points to a function instead of a variable.
c
CopyEdit
void (*func_ptr)(int); // Declare a function pointer
void print_num(int x) {
printf("%d\n", x);
}
func_ptr = print_num;
func_ptr(10); // Calls print_num with argument 10

Page 3: Pointers in C++


While pointers in C++ work similarly to C, C++ offers additional features such as classes and
new/delete operators for dynamic memory allocation.

Dynamic Memory Allocation in C++


• new operator allocates memory.

• delete operator frees memory.


cpp
CopyEdit
int *arr = new int[10]; // Dynamically allocate an array of 10 integers
arr[0] = 100; // Accessing the allocated memory

delete[] arr; // Free the dynamically allocated memory

Pointers to Classes in C++


Pointers can also point to class objects, and they are essential for dynamic object creation.
cpp
CopyEdit
class MyClass {
public:
int x;
void display() {
printf("%d\n", x);
}
};

int main() {
MyClass *obj = new MyClass(); // Dynamically create an object
obj->x = 50;
obj->display();
delete obj; // Free dynamically allocated object
}

Smart Pointers (C++11 and Later)


In modern C++, we often use smart pointers (std::unique_ptr, std::shared_ptr)
instead of raw pointers to automatically manage memory and avoid memory leaks.
cpp
CopyEdit
#include <memory>
std::unique_ptr<int> ptr = std::make_unique<int>(10); // Creates a smart
pointer

Page 4: Pointer Arithmetic


Pointer Arithmetic in C and C++
• Pointers can be incremented or decremented to move through an array.
• The size of data types is taken into account automatically.
c
CopyEdit
int arr[] = {10, 20, 30, 40};
int *ptr = arr; // Points to the first element

printf("%d\n", *(ptr + 2)); // Access third element (30)

Example: Using Pointer Arithmetic with Arrays


c
CopyEdit
for(int i = 0; i < 4; i++) {
printf("%d ", *(arr + i)); // Prints each element using pointer arithmetic
}

• Pointer arithmetic works because the pointer is adjusted based on the size of the data type.
For example, if arr is an integer array, ptr + 1 advances ptr by the size of an integer.

Pointer Comparison
Pointers can be compared to check if they point to the same memory location.
c
CopyEdit
if(ptr1 == ptr2) {
printf("Pointers are equal\n");
}

Page 5: Common Pointer Problems and Best Practices


Dangling Pointers
A pointer that points to memory that has already been freed.
c
CopyEdit
int *ptr = malloc(sizeof(int));
free(ptr);
*ptr = 10; // Dangling pointer

• Best practice: After freeing memory, set the pointer to NULL to avoid using dangling
pointers.

Memory Leaks
• Memory leaks occur when dynamically allocated memory is not freed.
• Best practice: Always free() memory when you are done using it.

Segmentation Faults
• Dereferencing uninitialized or NULL pointers causes segmentation faults.
• Best practice: Always initialize pointers and check for NULL before dereferencing.

Pointer Best Practices


• Always initialize pointers.
• Use new and delete carefully in C++.

• Consider using smart pointers in C++ to manage memory automatically.

Conclusion
• Pointers are one of the most powerful yet complex features in C and C++ programming.
• Understanding pointers is crucial for dynamic memory management, function references,
and efficient array handling.
• In C++, smart pointers help manage memory automatically, reducing errors like memory
leaks and dangling pointers.

You might also like