This is the mail archive of the glibc-linux@ricardo.ecn.wfu.edu mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

malloc is thrashing my system, please HELP


Please anyone help,

I do not know what is going wrong. I am trying to allocate memory until
malloc cannot fullfill my requests anymore. Instead of returning a NULL
pointer my system freezes.

Am I doing something stupid anyway? What can I do to prevent this case?
BTW on a system without virtual memory, I get a bus error instead.

Can I use a 'Low water mark' or something else to stop requesting memory,
before malloc would return NULL?

The program is:

#include <stdio.h>
#include <malloc.h>

struct list {
    struct list* pNext;
};

int main(int argc, char* argv[]) {
    struct list* pTop;
    struct list* pBottom;
    struct list* pl;

    pTop = malloc(sizeof(struct list) + 4096);
    pTop->pNext = NULL;
    pBottom = pTop;

    while((pl = malloc(sizeof(struct list) + 4096))) {
        pl->pNext = NULL;
        pBottom->pNext = pl;
        pBottom = pl;
    }

    printf("exhausted\n");

    pl = pTop;
    while (pl) {
        pl = pl->pNext;
        free(pl);
    }

    printf("freed\n");
    return 0;
}




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]