Thread: running out of memory

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    1

    running out of memory

    For a large program that does lots of data crunching (calculations) and lots of mallocs:

    I have tried doing the cleanlist function below after I finish using the list created by each malloc.
    However, nonetheless, I reach error 2 below ("Could not allocate memory - 2") after about 6 iterations
    of an intensive calcuation loop. The address is the empty "oxc000 etc." one that cannot be accessed (i.e.,
    malloc cannot allocate memory for the new listnode - I tested this by asserting that the listnode != NULL)).
    I think I may be running out of heap memory.

    Can you suggest a workaround (e.g., an alternative to malloc) or perhaps something that I should fix?
    The code I use for defining, creating and deallocating the list is shown below.

    NOTE: I am using ANSI C with MS Visual Studio

    Thanks!



    ///////////////////////////////CODE:

    //List definition

    typedef struct list_node_s{
    double p;
    double pc;
    double windowWeight;
    struct list_node_s *next;

    }list_node_t;


    typedef struct{

    list_node_t *head;
    double size;

    }ordered_list_t;


    //List Creation

    /************************************************** ************/
    list_node_t *
    insert(ordered_list_t * listp, double p, double pc, double windowWeight){

    ++(listp->size);

    listp->head = insert_in_order(listp->head,p,pc, windowWeight);

    return (listp->head);
    }
    /************************************************** ******/


    /************************************************** **********
    not sorted
    ************************************************** **********/
    list_node_t *
    insert_in_order(list_node_t* head, double p, double pc, double windowWeight){


    list_node_t * newnode = NULL;
    list_node_t * tail = NULL;
    list_node_t * headcopy = NULL;
    headcopy = head;
    tail = headcopy;


    /*empty list*/
    if(head == NULL){
    newnode = (list_node_t *)malloc(sizeof(list_node_t));
    //assert(newnode != NULL);
    if(newnode == NULL){
    printf("\nCOULD NOT ALLOCATE MEMORY - 1\n");
    }

    newnode->p = p;
    newnode->pc = pc;
    newnode->windowWeight = windowWeight;
    newnode->next = NULL;
    head = newnode;
    }



    else{

    newnode = (list_node_t *)malloc(sizeof(list_node_t));
    //assert(newnode != NULL);
    if(newnode == NULL){
    printf("\nCOULD NOT ALLOCATE MEMORY - 2\n"); //I GET THIS MESSAGE!!!!!!!!!!!!
    }
    newnode->p = p;
    newnode->windowWeight = windowWeight;
    tail = getTail(headcopy);
    pc = (p-(tail->p))/tail->p;
    newnode->pc = pc;
    newnode->next = NULL;
    tail->next = newnode;
    tail = tail->next;
    }


    return (head);

    }
    /************************************************** *********/


    //List Cleanup (freeing, etc):
    list_node_t * cleanlist(list_node_t * listToClean){

    if(listToClean == NULL)
    return NULL;

    //otherwise:

    list_node_t * temp = NULL;


    while(listToClean != NULL){

    temp = listToClean->next;

    free(listToClean);
    listToClean = temp;
    }

    //is this necessary? (SHOULD FREE THESE?)
    free(temp);
    listToClean = NULL;
    temp = NULL;

    return listToClean;

    }

    ////////////
    How they are used:

    I create the linked list as below:
    ////////
    list_node_t * createHistory(FILE * inp){

    ordered_list_t myList = {NULL,0};
    list_node_t * list;
    double p;
    char* tempStr= NULL;
    //printf("creating list\n");
    while(inp != NULL){
    //printf("creating list");
    //fscanf(inp, "%f", &p);

    tempStr = getstring(inp);
    if(tempStr != NULL){
    p = atof(tempStr);
    //printf("p is %f\n",p);
    }
    else{
    break;
    }
    list = insert(&myList,p,0.0,1.0);
    }
    //printf("List created and returning\n");
    return list;
    }

    /////////

    I use and dealloc the list as below:

    list_node_t * list = NULL
    list = createHistory(inp);
    double answer;
    answer = numberCrunchList(list, other stuff);
    list = cleanlist(list);
    return;

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    If you've read the rules wich you should before posting anything
    on the board you should've seen use code tags somewhere.
    --

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM
  2. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Detecting where an application is running in memory...
    By CompiledMonkey in forum C Programming
    Replies: 1
    Last Post: 08-12-2003, 01:25 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM