所以我每次都收到这个错误,我似乎无法弄清楚在哪里解决它。
这是它总是返回 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;
}
这是我的结构:
typedef struct _wordData
{
int iWordID;
char * cWord;
struct _wordData *next;
} wordData;
当我之前在我的列表中创建头节点时,完全相同的代码是如何工作的!编辑:阐述:
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;
}
所以上面的代码实际上创建了我的头节点。
编辑:关于内存:我有大量可用的内存:)
编辑:更多代码:
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;
}
我已经搜索了所有内容,但没有一个给出的解决方案对我有帮助,所以我们将不胜感激!