C 新线后的分段故障
我在**所附的线路上遇到了分段故障。 我将此输出导入管道: |状态:OK| |版本:0.85| |作者:PBrooks| |nitems:0| 在下面的代码中,当我打印printf语句中的“\n”后,它会给我一个分段错误。但是我不知道如何调试这个。。有人有线索吗C 新线后的分段故障,c,segmentation-fault,coredump,C,Segmentation Fault,Coredump,我在**所附的线路上遇到了分段故障。 我将此输出导入管道: |状态:OK| |版本:0.85| |作者:PBrooks| |nitems:0| 在下面的代码中,当我打印printf语句中的“\n”后,它会给我一个分段错误。但是我不知道如何调试这个。。有人有线索吗 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> char string
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char string[300];
struct NameValue {
char *name;
char *value;
};
struct NameValue *pairs;
void ReadStdin(int argc, char *argv) {
int x;
fread(string, sizeof (char), 300, stdin);
printf("%s\n", string);
}
void ParseInput() {
int x, num = 0; //figure out how many i need
for (x = 0; x < 300; x++) {
if (string[x] == '|') {
num++;
}
}
num /= 2; //num = how many i need
pairs = (malloc(num)); //allocate the array
int pipe = 0, i, j = 0, tempCounter = 0;
char tempName[50], tempValue[50];
printf("%lu \n", sizeof (string) / sizeof (string[0]));
if (pairs != 0) {
for (i = 0; i <= num; i++) { //counts pairs
printf("i = %i\n", i);
printf("j = %i, pipe: %i \n", j, pipe);
if (string[j] == '|') {
printf("there's a pipe\n");
pipe++;
j++;
}
while (string[j] != ':') {
printf("counter for main string: %i\n tempCounter: %i\n", j, tempCounter);
tempName[tempCounter] = string[j];
tempCounter++;
j++;
if (string[j] == ':') {
tempName[tempCounter] = '\0';
tempCounter = 0;
**printf("~~~~tempName\n is: %s", tempName);**
break;
}
}
while (string[j] != '|') {
j++;
tempValue[tempCounter] = string[j];
tempCounter++;
if (string[j] == '|') {
tempValue[tempCounter] = '\0';
tempCounter = 0;
strcpy(pairs[i].value, tempValue);
pipe++;
break;
}
}
}
}
}
int main(int argc, char *argv) {
ReadStdin(argc, argv);
ParseInput();
return (EXIT_SUCCESS);
}
#包括
#包括
#包括
#包括
字符串[300];
结构名称值{
字符*名称;
字符*值;
};
结构名称值*对;
void ReadStdin(int argc,char*argv){
int x;
fread(string,sizeof(char),300,stdin);
printf(“%s\n”,字符串);
}
void ParseInput(){
int x,num=0;//算出我需要多少
对于(x=0;x<300;x++){
如果(字符串[x]='|'){
num++;
}
}
num/=2;//num=我需要多少
pairs=(malloc(num));//分配数组
int管道=0,i,j=0,温度计数器=0;
字符tempName[50],tempValue[50];
printf(“%lu\n”,sizeof(string)/sizeof(string[0]);
如果(对!=0){
对于(i=0;i您从不使用null终止字符串tempName
。然后尝试在此处打印它:
if (string[j] == ':') {
tempCounter = 0;
**printf("~~~~tempName\n is: %s", tempName);**
break;
}
C中的字符串是内存中的一系列字符,以空字节终止(即'\0'
)。您正在将字节复制到缓冲区中,但您自己从未创建过此空终止符,因此printf
从末尾运行到未定义的内存中
相反,在while循环结束时,您需要将tempName
中的下一个字符指定给'\0'
,可能是在while循环内,然后再打印它
在tempCounter=0
之前,只需将tempName[tempCounter]='\0';
计算出来。我不仅需要malloc对,还需要malloc每个malloc对中的成员变量
最后,我用我的tempCounter查看了我需要多少字符,并为成员变量指定了正确的数量。对不起!我有那一行,但我忘了将其删除以进行测试。但它仍然会导致分段错误。你有其他想法吗?@Recur检查是否超出范围。逐步检查每一行代码。Wh我应该使用NetBeans进行调试吗?我不认为我可以使用NetBeans进行调试,因为我必须将一个程序的输出通过管道传输到我的程序中。我之前将其删除以进行测试,但我忘记将其放回。尽管如此,它仍然会导致分段错误。您还有其他想法吗?pairs=(malloc(num));//分配数组
什么是pairs?pairs是指向具有char*名称和值的结构的指针。然后我使用malloc将其指向一个数组(我的措辞可能不正确,但我希望您能理解)。我看到:它是一个全局指针。但是pairs=(malloc(num));//分配数组
,您的意图是:pairs=malloc(num*sizeof*pairs);
?我想为另一个程序的输出分配足够的对。在这种情况下,我得到了4行文本,所以我生成了4对malloc。错误:您使用了malloc()d四个字符。记住:malloc()以字符为单位计数。Malloc不知道您想要什么。它只以字符为单位计数。幸运的是,sizeof也以字符为单位计数。