Skip to content

Commit 3651fd2

Browse files
authored
Merge pull request codeharborhub#3800 from sivaprasath2004/sivaprasath-closes-issue-3791
[Feature]: Add C++ Language Course
2 parents 489f7fa + f664248 commit 3651fd2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3608
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
id: lesson-1
3+
title: "Advanced File Handling in C++"
4+
sidebar_label: Advanced File Handling
5+
sidebar_position: 1
6+
description: "Learn Advanced File Handling in C++"
7+
tags: [courses,Advance-level,Introduction]
8+
---
9+
10+
11+
Advanced file handling techniques allow for better control, performance optimization, and handling of large files. Let's explore these techniques:
12+
13+
### Flowchart
14+
15+
```mermaid
16+
graph TD;
17+
A[Start] --> B[File Locking Mechanisms];
18+
B --> C[File I/O Performance Optimization];
19+
C --> D[Handling Large Files];
20+
D --> E[Working with Memory-Mapped Files];
21+
E --> F[End];
22+
```
23+
24+
#### 1. File Locking Mechanisms
25+
26+
File locking prevents simultaneous access to a file, which can cause data corruption. It ensures that only one process can write to a file at a time.
27+
28+
##### Example: File Locking with `fcntl`
29+
30+
```cpp
31+
#include <iostream>
32+
#include <fcntl.h>
33+
#include <unistd.h>
34+
35+
int main() {
36+
int fd = open("example.txt", O_RDWR | O_CREAT, 0666);
37+
if (fd == -1) {
38+
std::cerr << "Failed to open file" << std::endl;
39+
return 1;
40+
}
41+
42+
struct flock lock;
43+
lock.l_type = F_WRLCK;
44+
lock.l_whence = SEEK_SET;
45+
lock.l_start = 0;
46+
lock.l_len = 0; // Lock the whole file
47+
48+
if (fcntl(fd, F_SETLK, &lock) == -1) {
49+
std::cerr << "Failed to lock file" << std::endl;
50+
close(fd);
51+
return 1;
52+
}
53+
54+
std::cout << "File locked. Press Enter to unlock..." << std::endl;
55+
std::cin.get();
56+
57+
lock.l_type = F_UNLCK;
58+
if (fcntl(fd, F_SETLK, &lock) == -1) {
59+
std::cerr << "Failed to unlock file" << std::endl;
60+
}
61+
62+
close(fd);
63+
return 0;
64+
}
65+
```
66+
67+
#### 2. File I/O Performance Optimization
68+
69+
Optimizing file I/O can significantly improve performance, especially when dealing with large files or high-frequency I/O operations.
70+
71+
:::tip
72+
- **Buffering**: Use buffered I/O for efficient data handling.
73+
- **Asynchronous I/O**: Perform non-blocking I/O operations to avoid waiting for I/O completion.
74+
- **Memory Mapping**: Use memory-mapped files for direct memory access to file contents.
75+
:::
76+
77+
#### 3. Handling Large Files
78+
79+
Handling large files efficiently is crucial for applications that deal with massive datasets.
80+
81+
##### Example: Reading Large Files in Chunks
82+
83+
```cpp
84+
#include <iostream>
85+
#include <fstream>
86+
87+
int main() {
88+
std::ifstream file("largefile.txt", std::ios::in | std::ios::binary);
89+
if (!file) {
90+
std::cerr << "Failed to open file" << std::endl;
91+
return 1;
92+
}
93+
94+
const size_t bufferSize = 1024 * 1024; // 1 MB buffer
95+
char buffer[bufferSize];
96+
97+
while (file.read(buffer, bufferSize) || file.gcount() > 0) {
98+
// Process buffer contents
99+
std::cout.write(buffer, file.gcount());
100+
}
101+
102+
file.close();
103+
return 0;
104+
}
105+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Advance File Handling",
3+
"position": 5,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Learn Advance File Handling."
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
id: lesson-2
3+
title: "Working with Memory-Mapped Files"
4+
sidebar_label: Working with Memory-Mapped Files
5+
sidebar_position: 2
6+
description: "Learn Working with Memory-Mapped Files"
7+
tags: [courses,Advance-level,Introduction]
8+
---
9+
10+
11+
Memory-mapped files allow for efficient file access by mapping a file's contents to memory. This technique is useful for large files and random access patterns.
12+
13+
##### Example: Memory-Mapped File Access
14+
15+
```cpp
16+
#include <iostream>
17+
#include <fcntl.h>
18+
#include <sys/mman.h>
19+
#include <sys/stat.h>
20+
#include <unistd.h>
21+
22+
int main() {
23+
int fd = open("largefile.txt", O_RDONLY);
24+
if (fd == -1) {
25+
std::cerr << "Failed to open file" << std::endl;
26+
return 1;
27+
}
28+
29+
struct stat sb;
30+
if (fstat(fd, &sb) == -1) {
31+
std::cerr << "Failed to get file size" << std::endl;
32+
close(fd);
33+
return 1;
34+
}
35+
36+
size_t fileSize = sb.st_size;
37+
char *mapped = static_cast<char *>(mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0));
38+
if (mapped == MAP_FAILED) {
39+
std::cerr << "Failed to map file" << std::endl;
40+
close(fd);
41+
return 1;
42+
}
43+
44+
// Access the file content through the mapped memory
45+
std::cout.write(mapped, fileSize);
46+
47+
if (munmap(mapped, fileSize) == -1) {
48+
std::cerr << "Failed to unmap file" << std::endl;
49+
}
50+
51+
close(fd);
52+
return 0;
53+
}
54+
```
55+
:::tip
56+
- **File Locking**: Use file locking mechanisms to prevent concurrent write operations.
57+
- **Buffered I/O**: Implement buffering to reduce the number of I/O operations.
58+
- **Asynchronous I/O**: Utilize asynchronous I/O for non-blocking operations.
59+
- **Memory Mapping**: Use memory-mapped files for efficient access to large files.
60+
:::
61+
62+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Advanced Memory Allocation",
3+
"position": 2,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Learn Advanced Memory Allocation."
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
id: lesson-2
3+
title: "Custom Allocators"
4+
sidebar_label: Custom Allocators
5+
sidebar_position: 2
6+
description: "Learn Custom Allocators"
7+
tags: [courses,Advance-level,Introduction]
8+
---
9+
10+
**Custom Allocators**: Custom allocators provide control over memory allocation strategies and can be used to optimize memory usage for specific applications.
11+
12+
##### Example: Custom Allocator
13+
14+
```cpp
15+
#include <iostream>
16+
#include <memory>
17+
#include <vector>
18+
19+
template<typename T>
20+
class CustomAllocator : public std::allocator<T> {
21+
public:
22+
typedef T value_type;
23+
24+
T* allocate(std::size_t n) {
25+
std::cout << "Allocating " << n << " elements." << std::endl;
26+
return std::allocator<T>::allocate(n);
27+
}
28+
29+
void deallocate(T* p, std::size_t n) {
30+
std::cout << "Deallocating " << n << " elements." << std::endl;
31+
std::allocator<T>::deallocate(p, n);
32+
}
33+
};
34+
35+
int main() {
36+
std::vector<int, CustomAllocator<int>> vec;
37+
vec.push_back(1);
38+
vec.push_back(2);
39+
vec.push_back(3);
40+
41+
std::cout << "Vector size: " << vec.size() << std::endl;
42+
return 0;
43+
}
44+
```
45+
46+
**Output:**
47+
```
48+
Allocating 1 elements.
49+
Allocating 1 elements.
50+
Allocating 1 elements.
51+
Vector size: 3
52+
Deallocating 1 elements.
53+
Deallocating 1 elements.
54+
Deallocating 1 elements.
55+
```
56+
57+
### Memory Alignment and Padding
58+
59+
**Memory Alignment and Padding**: Memory alignment refers to arranging data in memory to meet certain hardware requirements. Padding is used to align data structures in memory.
60+
61+
##### Example: Memory Alignment
62+
63+
```cpp
64+
#include <iostream>
65+
#include <cstddef> // For offsetof
66+
67+
struct AlignedStruct {
68+
char a;
69+
int b;
70+
double c;
71+
};
72+
73+
int main() {
74+
std::cout << "Size of AlignedStruct: " << sizeof(AlignedStruct) << std::endl;
75+
std::cout << "Offset of 'a': " << offsetof(AlignedStruct, a) << std::endl;
76+
std::cout << "Offset of 'b': " << offsetof(AlignedStruct, b) << std::endl;
77+
std::cout << "Offset of 'c': " << offsetof(AlignedStruct, c) << std::endl;
78+
79+
return 0;
80+
}
81+
```
82+
83+
**Output:**
84+
```
85+
Size of AlignedStruct: 16
86+
Offset of 'a': 0
87+
Offset of 'b': 4
88+
Offset of 'c': 8
89+
```
90+
91+
92+
93+
:::tip
94+
- **Memory Pools**: Useful for scenarios with frequent allocation and deallocation. They reduce fragmentation and improve performance.
95+
- **Smart Pointers**: Prefer using `unique_ptr` for single ownership, `shared_ptr` for shared ownership, and `weak_ptr` for non-owning references.
96+
- **Custom Allocators**: Utilize custom allocators for specialized memory management needs, especially in performance-critical applications.
97+
- **Memory Alignment**: Ensure data structures are aligned properly to meet hardware requirements and avoid performance penalties.
98+
:::

0 commit comments

Comments
 (0)