https://github.com/josh-audio/malloc
A memory allocation simulator
https://github.com/josh-audio/malloc
education malloc react visualization
Last synced: 9 months ago
JSON representation
A memory allocation simulator
- Host: GitHub
- URL: https://github.com/josh-audio/malloc
- Owner: josh-audio
- License: mit
- Created: 2020-01-01T02:01:22.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-04-03T03:50:59.000Z (10 months ago)
- Last Synced: 2025-04-30T05:09:31.854Z (9 months ago)
- Topics: education, malloc, react, visualization
- Language: TypeScript
- Homepage: https://josh-audio.github.io/malloc-new-2/
- Size: 592 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Malloc

A visualizer to help explain `malloc()` and related concepts.
A live version can be found [here](https://josh-audio.github.io/malloc).
## Usage
The interface displays a 256-byte array of memory cells. The first three are reserved, and have the following uses:
- 0x00 is `nullptr`, and so is unused
- 0x01 is a pointer to the first item in the free list
- 0x02 is a pointer to the next free item, if the `NEXT_FIT` allocation strategy is being used.
Memory cells can be edited by clicking and typing.
Hovering over reserved cells may show hints based on their meaning:
- Hovering a reserved cell that is a pointer to another cell will highlight the destination cell.
- Hovering a reserved cell that represents a block size will highlight the block.
Below the memory visualizer is a command interpreter. This interpreter allows a limited set of C-style statements.
Expressions will output their result. For example:
```c
5;
// -> 5
2 + 3;
// -> 5;
```
The interpreter supports variable declaration and assignment:
```c
int value = 5;
int otherValue = value + 10; // -> 15
```
Memory can be referenced and modified using pointers:
```c
char *a = 0xA; // pointer to memory address 0xA
char *b = a + 1; // pointer to memory address 0xB
*a = 5;
*b = 10;
*a + *b; // -> 15
```
Memory can be allocated and freed using `malloc()` and `free()`:
```c
int *a = malloc(sizeof(int));
*a = 0xFFFF1234;
free(a);
double *b = malloc(sizeof(double) * 2);
b[0] = 1.2;
b[1] = 2.3;
free(b);
```
There are also string manipulation functions. They are analogous to the equivalent C functions, though the behavior is not completely one-to-one. This is because, unlike heap memory (the 256 bytes on screen), stack memory does not have a 1:1 byte representation. This means that while heap strings behave like C strings, stack strings do not, and the string functions take this into account.
Here are some things you can do with strings:
```c
// This is valid C, and also works in the simulator:
char *a = malloc(strlen("hello") + 1);
strcpy(a, "hello");
// This is not valid C, but works in the simulator:
string myString = "hello";
char *b = malloc(strlen(myString) + 1);
strcpy(b, myString);
// If you want to read out a string, there is a convenience function for it:
getString(a); // -> "hello"
// strcpy will happily write past the allocated bounds:
strcpy(a, "this is way too long");
```
Besides the functions above, there are a few more helper functions that can be used from within the interpreter:
- `reset()`: Resets the memory to its original state
- `clear()`: Clears the command history
- `setDisplayBase(base)`: Sets the display base for the memory visualization; accepts either `10` or `16`. Default is `10`.
- `setStrategy(strategy)`: Sets the memory allocation strategy. Accepts `FIRST_FIT`, `NEXT_FIT`, `WORST_FIT` or `BEST_FIT`.
- `setCoalesceAfterFree(value)`: Sets whether coalesce happens automatically after `free()`.
- `coalesce()`: Coalesces fragmented memory. This happens automatically, unless `setCoalesceAfterFree(false)` has been called.
- `strlen()` and `strcpy()`: See above.
- `getString(address)`: Prints the string at the given address to the console.
- `save("some key")` and `load("some key")`: Saves and loads memory states, local scope, and settings. Persists between browser sessions.
## Development
To develop:
1. Clone the repo
2. Run `npm install`
3. Run `npm run dev`
To rebuild the nearley grammar:
1. Run `npm install -g nearley`
2. In the repository root, run `node --experimental-strip-types compile-grammar.ts`