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;