C 分段故障核心生成

C 分段故障核心生成,c,C,//我给strbrk打了三次电话,但我需要检查一下是不是!=空,怎么能做到呢 if(strpbrk(posfind,"<") != NULL ){ posfind =(char*)malloc(strlen(strpbrk(posfind,"<"))+strlen(posfind)*sizeof(char*)); posfind =strcat(strpbrk(posfind,"<"),posfind); } 如果(strpbrk(posfind),strca

//我给strbrk打了三次电话,但我需要检查一下是不是!=空,怎么能做到呢

if(strpbrk(posfind,"<") != NULL ){
    posfind =(char*)malloc(strlen(strpbrk(posfind,"<"))+strlen(posfind)*sizeof(char*));
    posfind =strcat(strpbrk(posfind,"<"),posfind);
}
如果(strpbrk(posfind),
strcat
没有为您分配新内存,您需要确保在调用它之前有足够的空间。在调用
strcat
时,您的空间似乎用完了,因此*WHAM*


在更新的示例中,使用一些临时变量来存储strpbrk(posfind)的结果,
strpbrk(posfind),因为您没有提供任何有用的代码。这些函数的作用是什么?我怀疑这是否编译,您有语法错误(在
返回0时缺少分号),
chkTag
未定义。chkTag只检查标记是否正确且有效。如果您没有提供实际的可编译示例,我们只能猜测。此外,如果您想知道程序的错误位置,答案很简单:使用调试器。如果我不知道需要多少空间,malloc怎么能使用?使用
strlen
并计算数量如果必须,可以在临时变量中存储一些字符串。posfind=(char*)malloc。
char* temp = strpbrk(posfind, "<");
char* newstring = NULL;
if (temp != NULL) {
    // You had a typo with the size, and also don't forget to add a spot for the
    // terminating null character
    newstring = malloc((strlen(temp) + strlen(posfind) + 1) * sizeof(char));
    newstring = strcpy(newstring, temp);
    newstring = strcat(newstring, posfind);
    posfind = newstring;
}