Skip to content

Commit

Permalink
day-12 content added (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhaararoraa authored Jun 12, 2024
1 parent 11ce811 commit be99020
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
71 changes: 71 additions & 0 deletions docs/day-12/dynamic-array_and_smart-pointer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
sidebar_position: 2
title: "Memory Management - Dynamic Arrays and Smart Pointers"
description: "In this tutorial, we will learn about dynamic arrays and smart pointers in C++ programming with the help of examples. Dynamic arrays allow flexible array sizing, and smart pointers manage memory automatically."
sidebar_label: "Dynamic Arrays and Smart Pointers"
slug: memory-management-dynamic-arrays-smart-pointers
---

## Dynamic Arrays

Dynamic arrays are arrays whose size can be determined at runtime. They are created using the `new` operator and must be deallocated using the `delete` operator to avoid memory leaks.

- **Creating Dynamic Arrays:**
- Syntax:
```cpp
type* array = new type[size];
```
- Example:
```cpp
int size = 5;
int* dynamicArray = new int[size];
```

- **Accessing and Modifying Elements:**
- Elements are accessed and modified using pointers.
- Example:
```cpp
dynamicArray[0] = 1; // Assigns value to the first element
int value = dynamicArray[1]; // Retrieves value of the second element
```

- **Deallocating Dynamic Arrays:**
- Syntax:
```cpp
delete[] array;
```
- Example:
```cpp
delete[] dynamicArray;
```

## Smart Pointers

Smart pointers are a feature of the C++ Standard Library that help manage dynamic memory by ensuring proper deallocation. They are part of the `<memory>` header.

- **Types of Smart Pointers:**
- **`std::unique_ptr`:**
- Ensures that only one pointer can own a resource at a time.
- Automatically deletes the resource when the `unique_ptr` goes out of scope.
- Syntax:
```cpp
std::unique_ptr<int> ptr(new int(10));
```

- **`std::shared_ptr`:**
- Allows multiple pointers to share ownership of a resource.
- The resource is deleted when the last `shared_ptr` pointing to it is destroyed.
- Syntax:
```cpp
std::shared_ptr<int> ptr1 = std::make_shared<int>(10);
std::shared_ptr<int> ptr2 = ptr1; // Shared ownership
```

- **`std::weak_ptr`:**
- Holds a non-owning reference to a resource managed by `std::shared_ptr`.
- Used to break cyclic dependencies between `shared_ptr`s.
- Syntax:
```cpp
std::weak_ptr<int> weakPtr = sharedPtr;
```

39 changes: 39 additions & 0 deletions docs/day-12/memoryleak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
sidebar_position: 3
title: "Memory Management - Memory Leaks and How to Avoid Them"
description: "In this tutorial, we will learn about memory leaks in C++ programming, how to identify them, and best practices to avoid them. Proper memory management is crucial for efficient and reliable software."
sidebar_label: "Memory Leaks and How to Avoid Them"
slug: memory-management-memory-leaks-avoidance
---

## Memory Leaks and How to Avoid Them

Memory leaks occur when dynamically allocated memory is not deallocated, leading to wasted memory resources. They can cause programs to consume more memory over time, potentially leading to system crashes or slowdowns.

- **Identifying Memory Leaks:**
- Use memory profiling tools like Valgrind, Dr. Memory, or built-in tools in IDEs to detect leaks.
- Regularly review code to ensure every `new` has a corresponding `delete`.

- **Avoiding Memory Leaks:**
- **Always Pair `new` with `delete`:**
- Ensure that every allocation using `new` or `new[]` is paired with a corresponding `delete` or `delete[]`.
- **Use RAII (Resource Acquisition Is Initialization):**
- Tie resource management to object lifetimes. For example, use stack-allocated objects whose destructors handle resource cleanup.
- Example:
```cpp
class Resource {
public:
Resource() { data = new int[100]; }
~Resource() { delete[] data; }
private:
int* data;
};
```
- **Utilize Smart Pointers:**
- Prefer using smart pointers (`std::unique_ptr`, `std::shared_ptr`) which automatically manage memory.
- Example:
```cpp
std::unique_ptr<int> ptr(new int(10)); // Memory automatically managed
```

By understanding and properly using these tools and techniques, you can effectively manage memory in your C++ programs, avoid common pitfalls, and ensure efficient use of resources.
43 changes: 43 additions & 0 deletions docs/day-12/new_and_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
sidebar_position: 1
title: "Memory Management - new and delete Operators"
description: "In this tutorial, we will learn about `new` and `delete` operators in C++ programming with the help of examples. These operators are used for dynamic memory allocation and deallocation in C++."
sidebar_label: "New and Delete Operators"
slug: memory-management-new-delete-operators
---


## Memory Management in C++
Memory management is a critical aspect of programming in C++, as it involves allocating and deallocating memory dynamically at runtime. Proper memory management ensures that programs run efficiently and avoid issues such as memory leaks, fragmentation, and corruption.

#### `new` and `delete` Operators

The `new` and `delete` operators are fundamental for dynamic memory management in C++. They allow you to allocate and deallocate memory during runtime.

- **`new` Operator:**
- Allocates memory from the heap.
- Returns a pointer to the allocated memory.
- Syntax:
```cpp
type* pointer = new type; // For a single element
type* pointer = new type[size]; // For an array
```
- Example:
```cpp
int* p = new int; // Allocates memory for a single integer
int* arr = new int[10]; // Allocates memory for an array of 10 integers
```

- **`delete` Operator:**
- Deallocates memory that was previously allocated using `new`.
- Frees the memory, making it available for future allocations.
- Syntax:
```cpp
delete pointer; // For a single element
delete[] pointer; // For an array
```
- Example:
```cpp
delete p; // Deallocates memory for the single integer
delete[] arr; // Deallocates memory for the array of integers
```

0 comments on commit be99020

Please sign in to comment.