Skip to content

Commit f8c9361

Browse files
committed
feat: add cpp elementals tricks
1 parent 756ea1a commit f8c9361

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

website/docs/c-cpp/_category_.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"label": "C/C++",
3+
"position": 2,
4+
"link": {
5+
"type": "generated-index"
6+
}
7+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
sidebar_position: 1
3+
title: Compilation IO Guide
4+
sidebar_label: Compilation IO Guide
5+
---
6+
7+
## Compile and Execute C++ Program with Input and Output Redirection
8+
9+
**Standard**
10+
```bash
11+
g++-14 -std=c++17 -Wall -O2 -fno-sanitize-recover -o A A.cpp
12+
```
13+
14+
**Debug**
15+
```bash
16+
g++-14 -std=c++17 -DDEBUG -Wall -O2 -fno-sanitize-recover -o A A.cpp
17+
```
18+
19+
**Running**
20+
```bash
21+
./A < A.in > A.out
22+
```
23+
24+
### Explanation
25+
This command compiles and executes a C++ program, redirecting the input and output to specific files. Below is a breakdown of the command: _(Useful for competitive programming)_
26+
27+
- `g++-14`: Specifies the version of the GNU C++ Compiler, in this case, version 14.
28+
- `-std=c++17`: Instructs the compiler to use the C++17 standard for compilation.
29+
- `-Wall`: Enables all compiler warnings, which helps in identifying potential issues.
30+
- `-O2`: Activates a moderate level of optimization to improve the performance of the compiled program.
31+
- `-fno-sanitize-recover`: Ensures the program stops execution immediately when encountering a sanitization error, instead of attempting to recover.
32+
- `-DDEBUG`: Defines the macro `DEBUG`, enabling any conditional compilation sections related to debugging within the source code.
33+
- `-o A`: Specifies that the output executable file will be named `A`.
34+
- `A.cpp`: The source file to be compiled.
35+
- `./A < A.in > A.out`: Executes the compiled program, reading input from the file `A.in` and writing the output to the file `A.out`.
36+
37+
This method is efficient for running programs with predefined input and capturing the output into a file for further analysis or documentation purposes.

website/docs/c-cpp/memory.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
sidebar_position: 1
3+
title: Memory Guide
4+
sidebar_label: Memory Guide
5+
---
6+
7+
## Limit Memory Usage with `ulimit` Command
8+
9+
The following commands allow you to limit the memory usage for your processes. These limits are specified in kilobytes (KB) and are useful for setting constraints during development, particularly for ensuring your programs do not exceed certain memory thresholds. Here’s how you can set and verify these limits:
10+
11+
```bash
12+
ulimit -v 65536 # Limit memory to 64 MB
13+
ulimit -v 131072 # Limit memory to 128 MB
14+
ulimit -v 262144 # Limit memory to 256 MB
15+
ulimit -v 524288 # Limit memory to 512 MB
16+
ulimit -v 1048576 # Limit memory to 1 GB
17+
ulimit -v 2097152 # Limit memory to 2 GB
18+
ulimit -v 4194304 # Limit memory to 4 GB
19+
ulimit -v 8388608 # Limit memory to 8 GB
20+
ulimit -v 16777216 # Limit memory to 16 GB
21+
ulimit -v 33554432 # Limit memory to 32 GB
22+
```
23+
24+
You may find more details in the `ulimit` manual page using the command:
25+
26+
```bash
27+
man ulimit
28+
```
29+
30+
### Verify Current Limits
31+
32+
To check the current resource limits, you can execute the following command:
33+
34+
```bash
35+
ulimit -a
36+
```
37+
38+
### Memory Limits Table
39+
40+
The following table provides a reference for memory limits in both megabytes (MB) and kilobytes (KB):
41+
42+
| Memory (MB) | Memory (KB) |
43+
|--------------|--------------|
44+
| 64 MB | 65,536 KB |
45+
| 128 MB | 131,072 KB |
46+
| 256 MB | 262,144 KB |
47+
| 512 MB | 524,288 KB |
48+
| 1 GB | 1,048,576 KB |
49+
| 2 GB | 2,097,152 KB |
50+
| 4 GB | 4,194,304 KB |
51+
| 8 GB | 8,388,608 KB |
52+
| 16 GB | 16,777,216 KB|
53+
| 32 GB | 33,554,432 KB|
54+
```

0 commit comments

Comments
 (0)