{"id":27776363,"url":"https://github.com/josh-audio/malloc","last_synced_at":"2025-04-30T05:09:42.943Z","repository":{"id":77979642,"uuid":"231165622","full_name":"josh-audio/malloc","owner":"josh-audio","description":"A memory allocation simulator","archived":false,"fork":false,"pushed_at":"2025-04-03T03:50:59.000Z","size":606,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-30T05:09:31.854Z","etag":null,"topics":["education","malloc","react","visualization"],"latest_commit_sha":null,"homepage":"https://josh-audio.github.io/malloc-new-2/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/josh-audio.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-01T02:01:22.000Z","updated_at":"2025-04-03T03:51:04.000Z","dependencies_parsed_at":"2025-02-25T04:41:50.869Z","dependency_job_id":"118168d5-6491-4683-b658-4b19ca8247dc","html_url":"https://github.com/josh-audio/malloc","commit_stats":null,"previous_names":["01jw/malloc-visualizer","josh-audio/malloc-visualizer","josh-audio/malloc","secondflight/malloc-visualizer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josh-audio%2Fmalloc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josh-audio%2Fmalloc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josh-audio%2Fmalloc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josh-audio%2Fmalloc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josh-audio","download_url":"https://codeload.github.com/josh-audio/malloc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251644842,"owners_count":21620634,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["education","malloc","react","visualization"],"created_at":"2025-04-30T05:09:42.525Z","updated_at":"2025-04-30T05:09:42.934Z","avatar_url":"https://github.com/josh-audio.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Malloc\n\n![screenshot](screenshot.png)\n\nA visualizer to help explain `malloc()` and related concepts.\n\nA live version can be found [here](https://josh-audio.github.io/malloc).\n\n## Usage\n\nThe interface displays a 256-byte array of memory cells. The first three are reserved, and have the following uses:\n- 0x00 is `nullptr`, and so is unused\n- 0x01 is a pointer to the first item in the free list\n- 0x02 is a pointer to the next free item, if the `NEXT_FIT` allocation strategy is being used.\n\nMemory cells can be edited by clicking and typing.\n\nHovering over reserved cells may show hints based on their meaning:\n- Hovering a reserved cell that is a pointer to another cell will highlight the destination cell.\n- Hovering a reserved cell that represents a block size will highlight the block.\n\nBelow the memory visualizer is a command interpreter. This interpreter allows a limited set of C-style statements.\n\nExpressions will output their result. For example:\n\n```c\n5;\n// -\u003e 5\n\n2 + 3;\n// -\u003e 5;\n```\n\nThe interpreter supports variable declaration and assignment:\n\n```c\nint value = 5;\nint otherValue = value + 10; // -\u003e 15\n```\n\nMemory can be referenced and modified using pointers:\n\n```c\nchar *a = 0xA; // pointer to memory address 0xA\nchar *b = a + 1; // pointer to memory address 0xB\n*a = 5;\n*b = 10;\n*a + *b; // -\u003e 15\n```\n\nMemory can be allocated and freed using `malloc()` and `free()`:\n\n```c\nint *a = malloc(sizeof(int));\n*a = 0xFFFF1234;\nfree(a);\n\ndouble *b = malloc(sizeof(double) * 2);\nb[0] = 1.2;\nb[1] = 2.3;\nfree(b);\n```\n\nThere 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.\n\nHere are some things you can do with strings:\n\n```c\n// This is valid C, and also works in the simulator:\nchar *a = malloc(strlen(\"hello\") + 1);\nstrcpy(a, \"hello\");\n\n// This is not valid C, but works in the simulator:\nstring myString = \"hello\";\nchar *b = malloc(strlen(myString) + 1);\nstrcpy(b, myString);\n\n// If you want to read out a string, there is a convenience function for it:\ngetString(a); // -\u003e \"hello\"\n\n// strcpy will happily write past the allocated bounds:\nstrcpy(a, \"this is way too long\");\n```\n\nBesides the functions above, there are a few more helper functions that can be used from within the interpreter:\n- `reset()`: Resets the memory to its original state\n- `clear()`: Clears the command history\n- `setDisplayBase(base)`: Sets the display base for the memory visualization; accepts either `10` or `16`. Default is `10`.\n- `setStrategy(strategy)`: Sets the memory allocation strategy. Accepts `FIRST_FIT`, `NEXT_FIT`, `WORST_FIT` or `BEST_FIT`.\n- `setCoalesceAfterFree(value)`: Sets whether coalesce happens automatically after `free()`.\n- `coalesce()`: Coalesces fragmented memory. This happens automatically, unless `setCoalesceAfterFree(false)` has been called.\n- `strlen()` and `strcpy()`: See above.\n- `getString(address)`: Prints the string at the given address to the console.\n- `save(\"some key\")` and `load(\"some key\")`: Saves and loads memory states, local scope, and settings. Persists between browser sessions.\n\n## Development\n\nTo develop:\n\n1. Clone the repo\n2. Run `npm install`\n3. Run `npm run dev`\n\nTo rebuild the nearley grammar:\n\n1. Run `npm install -g nearley`\n2. In the repository root, run `node --experimental-strip-types compile-grammar.ts`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosh-audio%2Fmalloc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosh-audio%2Fmalloc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosh-audio%2Fmalloc/lists"}