So I have been getting this error every time, I cant seem to figure out where to solve it.
This is where it always returns null:
wordData * ptr;
ptr = (wordData*)malloc(sizeof(wordData)); //returns null every time
if(NULL == ptr)
{
printf("\n Node creation failed in add list\n");
return NULL;
}
This is my struct:
typedef struct _wordData
{
int iWordID;
char * cWord;
struct _wordData *next;
} wordData;
How ever when I create the headnode in my list earlier on the exact same code works! EDIT: elaboration:
printf("\n creating list with headnode as [%d] %s\n",iWordID,cWord);
wordData *ptr;
ptr = (wordData*)malloc(sizeof(wordData));
if(NULL == ptr)
{
printf("\n Node creation failed in create list \n");
return NULL;
}
So the above code actually creates my headnode.
EDIT: on memory: I have loads of memory available :)
EDIT: more code:
void clearStr() // adding word into list with a struct containing word,wordID
{
add_to_list(iID,cStr,true);
memset(cStr, '\0', sizeof(cStr) );
iCounter = 0;
}
wordData* add_to_list(int iWordID, char * cWord, bool add_to_end)
{
char temp[strlen(cWord)+1];
strcpy(temp, cWord);
if(NULL == head)
{
return (create_list(iWordID, temp));
}
if(add_to_end)
printf("\n Adding node to end of list with iWordID [%d] %s\n",iWordID, cWord);
else
printf("\n Adding node to beginning of list with iWordID [%d]\n",iWordID);
int sizeWordData = sizeof(wordData);
printf("Size wordData is: %d",sizeWordData); //12 every time
wordData * ptr;
ptr = (wordData*)malloc(sizeof(wordData)); //waarom is dit null?
if(NULL == ptr)
{
printf("\n Node creation failed in add list\n");
return NULL;
}
ptr->iWordID = iWordID;
ptr -> cWord,temp;
strcpy(ptr -> cWord, temp);
ptr->next = NULL;
if(add_to_end)
{
curr->next = ptr;
curr = ptr;
printf("pointers geset");
}
else
{
ptr->next = head;
head = ptr;
}
return ptr;
}
I have searched everything but none of the give solutions have helped me, so help would be appreciated!