Altera_Forum
Honored Contributor
14 years agoMalloc till it fails, crashes, bug in Malloc?
I'm trying to write some code that will determin how much "free" memory the system still has. I created some somewhat speedy code, but really the simple code could just run a loop like this:
uint32_t FreeMemory(void) { uint8_t *ptr; uint32_t counter = 1; ptr = malloc(counter) while (ptr != NULL) { counter++; free (ptr); ptr = malloc(counter); } free(ptr); return counter; } This can run a few times, but eventually the system crashes because of this. I have tried disabeling interupts, but it still crashes. Is there a bug with malloc? (new crashes as well) Or is this not possible to do? I realize I'm nearly using all free memory, but I also free it as soon as the function is finished? I really want some code that can run in the background to make sure they system does not have any memory leaks. This code is not running on any operating system. Anyone with expreince with this? Thanks!