Created
March 17, 2021 19:06
-
-
Save MichaelBell/a13513a483a07a41c5b74e8458fba51d to your computer and use it in GitHub Desktop.
Demonstration of malloc failure with 4KB heap left
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include "pico/stdlib.h" | |
| uint32_t use_almost_all_memory[63410]; | |
| int main() | |
| { | |
| stdio_init_all(); | |
| // Reference the big array so it isn't removed by the linker | |
| use_almost_all_memory[0] = 1; | |
| int i = 0; | |
| while (1) { | |
| void* ptr = malloc(36); | |
| if (!ptr) break; | |
| printf("Allocated pointer %p\n", ptr); | |
| ++i; | |
| } | |
| printf("Failed after %d allocations of 36 bytes\n", i); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment