C malloc始终返回null
所以我每次都会遇到这个错误,我似乎不知道该在哪里解决它 这就是它总是返回null的地方:C malloc始终返回null,c,pointers,null,malloc,C,Pointers,Null,Malloc,所以我每次都会遇到这个错误,我似乎不知道该在哪里解决它 这就是它总是返回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; } 这是我
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;
然而,当我在我的列表中创建headnode时,前面的代码完全相同!
编辑:精化:
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;
}
我已经搜索了所有内容,但是没有一个给出的解决方案对我有帮助,所以我将非常感谢您的帮助 恐怕你的问题中没有足够的信息来正确回答它 这: 最好写为:
ptr = malloc(sizeof *ptr);
它比较短,重复次数少,而且通常都比较成功
但是,由于您说相同的代码在程序流的早期工作,因此您的代码可能在这里做了正确的事情
您分配了多少个节点?是不是有些递归失控了,分配了数百万个节点,耗尽了内存
还请注意,您的标题与您的文本相矛盾,这使得这个问题更加混乱。从formalloc()
中有两次malloc()
可以返回NULL
:
- 如果大小为
它可以返回0
NULL
- 如果内存请求失败
当我之前在列表中创建headnode时,完全相同的代码是如何工作的
,这意味着您的示例中缺少了一些可能包含问题的内容:
ptr = (wordData*)malloc(sizeof(wordData));
^
this should never be 0, so that shouldn't be the problem
你的程序中有循环或递归调用吗?你分配了多少次
编辑:
您说您只是在读取文件,请尝试以下操作:
main()
open file
loop for length of file
allocate your memory
free memory
如果这个最小的代码示例失败,请发布代码和文本文件的示例,如果它有效,那么在您的较大程序中还有其他原因导致了问题,您可以从中“构建备份”,并可能找到它。您实际上已经在前面使用缓冲区溢出破坏了堆
strcpy(ptr -> cWord, temp);//without allocating anything to cWord
这将导致malloc行为不当。做:
ptr -> cWord = malloc(strlen(temp) +1);//allocate
strcpy(ptr -> cWord, temp);//then copy
你的平台是什么?你想分配多少这样的东西?是否真的内存不足?没有称为malloc.h的标准头。要使用malloc(),您应该只包含stdlib.h。可能会出现堆损坏,请尝试使用efence或等效工具运行。您是否使用
gcc-Wall…
进行编译?如果是,是否注意任何编译器警告?(如果没有,为什么没有?)您在结构的*cWord成员中存储了什么?每个malloc'ed结构一个单词?你在为它分配内存吗?你提到了从文件中读取-你能给我们更多的代码吗?看起来像堆损坏。大小是12,我这样做的每一个字,我从一个txt文件读取。该文件包含的内容不超过20个words@flexzican-是否有一个更完整(最少)的代码示例可供分享,以显示问题?我们可以运行它,看看结果是否相同?有关如何开始解决此问题的示例,请参见我文章中的编辑。
ptr -> cWord = malloc(strlen(temp) +1);//allocate
strcpy(ptr -> cWord, temp);//then copy