Skip to content

Instantly share code, notes, and snippets.

@MichaelBell
Created March 17, 2021 19:06
Show Gist options
  • Select an option

  • Save MichaelBell/a13513a483a07a41c5b74e8458fba51d to your computer and use it in GitHub Desktop.

Select an option

Save MichaelBell/a13513a483a07a41c5b74e8458fba51d to your computer and use it in GitHub Desktop.
Demonstration of malloc failure with 4KB heap left
#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