In a loop I allocate some memory at the beginning (probably up to 2-3k on a Linux ARM), work on it and then deallocate it. Since the loop has many iterations, I am wondering if such allocation/deallocation will fragment the heap so a subsequent "malloc" will fail.
The pseudo-code looks like this:
...
while(...){
list=malloc(N);
// do some work with list
free(list);
}
No, that's perfectly fine. Also heap fragmentation starts to be an issue after many more allocations - of different sizes. Also, if you are still worried, just use alloca on the stack for allocations like in your loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.