Hi.
Is there a memory allocation bug in gcc? I'm running Linux
1.2.13, gcc 2.7 and libc 5.0.9. If I compile and run the small
program below, malloc (or new, if I use g++) manages to allocate much
more memory that is available (including virtual memory). For
example, here I have 40MB of physical memory and 32MB of swap space,
but malloc manages to allocate 1143 blocks of 1MB before returning
NULL!!! Where is this memory coming from?
------------%< Beginning of code %<----------
#include <stdlib.h>
#define NB 120
#define BLOCK 1024*1024
char *iv[NB];
main()
{
int i,j;
for (i=0; i<NB; i++) {
printf("%d ", i);
iv[i] = (char *)malloc(BLOCK);
if (iv[i]==NULL) {
printf("out of memory \n");
exit(-1);
}
}
for (;;) {
for (i=0; i<NB; i++)
for (j=0; j<BLOCK; j++)
iv[i][j] = iv[i][j] + 1;
}
------------%< end of code %<-----------Quote:}
On the code above, if I set NB to anything below 1143, the
program runs without segmentation faults or other errors. Linux
starts to swap heavily, and becomes almost unusable, but doesn't die.
However, if I request only one large block, malloc correctly
returns NULL if the block is bigger than the available memory.
Is this a known bug? Must I upgrade immediatly?
Many thanks in advance.
Celso.