https://github.com/varugasu/mem-mgmt
https://github.com/varugasu/mem-mgmt
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/varugasu/mem-mgmt
- Owner: varugasu
- License: mit
- Created: 2024-12-30T18:48:57.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-12-30T19:16:58.000Z (4 months ago)
- Last Synced: 2024-12-30T20:23:04.143Z (4 months ago)
- Language: C++
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Custom Memory Managament
## Stack Allocator
```cpp
memory = static_cast(std::malloc(size_bytes));
```We cast to `char*`:
- `char` is 1 byte in size
- Allow byte-precise pointer arithmethic. Like we do on `void* block = memory + top;`
- The C++ convetion for representing raw memoryThe byte-previde pointer arithmethic is crucial because we know exactly how much we advance our pointer.
```cpp
char* memory = static_cast(malloc(100));
memory + 1 // Advances exactly 1 byte
memory + 50 // Advances exactly 50 bytes
```If we use another type, for example `int*`, we must **multiply by 4**, because `sizeof(int) = 4`:
```cpp
int* memory = static_cast(malloc(100));
memory + 1 // Advances 4 bytes
memory + 50 // Advances 200 bytes
```