c++ - Why does malloc() or new never return NULL? -


this question has answer here:

i'm writing application needs lot of memory caching purposes described here. i'm playing around malloc / new constructions figure out how realise it. made strange observation:

#include <stdio.h> #include <stdlib.h>  int main(void) {   while(1) {     char *foo = (char*)malloc(1024);// new char[1024];     if(foo == null) {        printf("couldn't alloc\n");       fflush(stdout);       return 0;     }   }   return 0; } 

why printf never reached? if system runs out of memory, malloc said return null, explained here. receive sigkill (i'm using linux...).

linux, default, uses opportunistic memory allocation scheme, meaning kernel give valid address won't allocated until first use.

see:

according responses can turn feature off using echo 2 > /proc/sys/vm/overcommit_memory.

from can tell, done under assumption wont use memory allocate. can't ever allocate space don't touch @ least once, i'd curious know how affects real life performance...

regarding sigkill failure, every malloc call still allocating memory each call. fill memory malloc overhead , evoke fury of out of memory kill feature. whether alone issue or if perhaps overcommit policy still allocates fraction of requested space question.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -