Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为链表节点中的字符串动态分配内存_C_String_Visual Studio 2010_Linked List_Calloc - Fatal编程技术网

C 为链表节点中的字符串动态分配内存

C 为链表节点中的字符串动态分配内存,c,string,visual-studio-2010,linked-list,calloc,C,String,Visual Studio 2010,Linked List,Calloc,我正在使用VisualStudio2010,我知道它有一些特性。我希望不是这样 这显然是一个更大计划的一部分,但我已经尝试过简化它,这样我就可以知道我在做什么 每次我运行它时,calloc分配解析为NULL,我退出程序。我在没有calloc的if语句的情况下尝试了它,但得到了一个调试错误,所以我很确定问题出在calloc上 任何帮助都将不胜感激 #include <stdio.h> #include <string.h> #include <stdlib.h>

我正在使用VisualStudio2010,我知道它有一些特性。我希望不是这样

这显然是一个更大计划的一部分,但我已经尝试过简化它,这样我就可以知道我在做什么

每次我运行它时,calloc分配解析为NULL,我退出程序。我在没有calloc的if语句的情况下尝试了它,但得到了一个调试错误,所以我很确定问题出在calloc上

任何帮助都将不胜感激

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct NODE {
    char * x;
    struct NODE * link;
} NODE;

typedef struct {
    NODE * first;
} STRUCTURE;

NODE * insertNode (NODE * pList, NODE * pPre, char * string);

int main (void) {
   STRUCTURE  str[2];
   char * string = "blah";
    str[2].first = NULL;
    str[2].first = insertNode (str[2].first, str[2].first, string); 
    printf ("\n%s\n", str[2].first->x);
return 0;
}

NODE * insertNode (NODE * pList, NODE * pPre, char * string)
{
    //Local Declarations
    NODE * pNew;

    //Statements
    if ( !(pNew = (NODE*)malloc(sizeof(NODE)))) 
            printf ("\nMemory overflow in insert\n"),
                exit (100);
        if ( ( pNew->x = (char*)calloc((strlen(string) + 1), sizeof(char))) ==     NULL);
        {
            printf ("\nMemory overflow in string creation\n");
            exit (100);
        }
        strncpy(pNew->x, string, strlen(pNew->x)); 
    if (pPre == NULL) //first node in list
    {
        pNew->link = pList;
        pList = pNew;
    }
    else 
    {
        pNew->link = pPre->link;
        pPre->link = pNew;
    }

    return pList;
}
#包括
#包括
#包括
类型定义结构节点{
char*x;
结构节点*链接;
}节点;
类型定义结构{
节点*第一;
}结构;
节点*插入节点(节点*pList,节点*pPre,字符*字符串);
内部主(空){
结构str[2];
char*string=“blah”;
str[2]。first=NULL;
str[2]。first=insertNode(str[2]。first,str[2]。first,string);
printf(“\n%s\n”,str[2].first->x);
返回0;
}
节点*插入节点(节点*pList,节点*pPre,字符*string)
{
//本地声明
节点*pNew;
//声明
如果(!(pNew=(NODE*)malloc(sizeof(NODE)))
printf(“\n插入中内存溢出\n”),
出口(100);
如果((pNew->x=(char*)calloc((strlen(string)+1),sizeof(char))==NULL;
{
printf(“\n创建字符串时内存溢出\n”);
出口(100);
}
strncpy(pNew->x,string,strlen(pNew->x));
if(pPre==NULL)//列表中的第一个节点
{
pNew->link=pList;
pList=pNew;
}
其他的
{
pNew->link=pPre->link;
pPre->link=pNew;
}
返回层;
}
我正在使用VisualStudio2010,我知道它有一些特性。我希望不是这样

它是一个分号:

if ( ( pNew->x = (char*)calloc((strlen(string) + 1), sizeof(char))) ==     NULL);
                                                                                ^
无论
calloc
返回什么,都将进入以下块,您将调用
exit


旁注,你可以这样写:

if (!(pNew->x = calloc(strlen(string) + 1, 1)))
    /* ... */

隐马尔可夫模型。。。修正了这个问题,但是现在它抛出了一个调试错误。运行时检查失败#2-变量“str”周围的堆栈已损坏。