Date: Wed, 3 Jun 92 17:12:03 CDT From: daniel AT asd470 DOT dseg DOT ti DOT com (Daniel Lemon) To: djgpp AT sun DOT soe DOT clarkson DOT edu Subject: malloc() doesn't return a null pointer when out of memory Status: O I tried compiling the following program with djgcc: ========================== /* memory.c - allocate memory until out */ struct item { int number; char string[100000]; struct item *next; } *list; main () { struct item *ptr; int i=0; list = ptr = (struct item *) malloc (sizeof (struct item)); while (ptr) { ptr->number = i++; printf ("Current pointer: %p Number of objects: %d Memory used: %d\n", ptr, i, i * sizeof (struct item)); ptr = ptr->next = (struct item *) malloc (sizeof (struct item)); } printf ("Current pointer: %p Number of objects: %d Memory used: %d\n", ptr, i, i * sizeof (struct item)); } ========================= It is supposed to keep allocating memory until there is no more memory left, at which point I expected to get a NULL pointer returned from malloc(). Instead, I got this error: Fatal! disk full writing to swap file When I increased the size of the array item.string to 1 000 000 or 10 000 000, my PC froze and I had to reboot. When I increased it to 100 000 000, I got the error Segmentation violation in pointer 0xe8421000 at 40:1318 Exception 14 at eip=1318 Shouldn't malloc just return a NULL pointer? I have some memory-hogging programs that depend on that for error checking.