[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

malloc problem



i dont know if anyone else has had this problem before: 

if you malloc a certain amount of memory, sometimes, the malloc succeeds, 
but the process is killed: 

Out of Memory: Killed process 163 (testm).
Terminated                           

for example, a program such as this: 
main(int argc, char *argv[])
{
        int *dataptr;
        unsigned long i, n;

        n = atoi(argv[1]);
        n = n*n;

        printf ("allocating %ld ints\n", n);
        dataptr = (int *)malloc(sizeof(int) * n);
        if (!dataptr)
        {
                printf ("malloc failed\n");
                return;
        }
        printf ("malloc succeeded\n");
        for (i = 0; i < n; i++)
                dataptr[i] = i;
        free(dataptr);
}

does anyknow know why this happens and how to prevent this? 

TIA, 
akshay