gpt4 book ai didi

c - 包含 会导致段错误

转载 作者:行者123 更新时间:2023-11-30 14:30:29 26 4
gpt4 key购买 nike

我正在编写一个程序来从文本文件中读取信息,并且一切正常。问题是我正在尝试添加功能来计算某些字段的平均值,并且必须将字符串转换为 double 。我注意到 atof 在某些情况下可以工作,但大多数情况下会返回 -1。然后我意识到我没有包含 stdlib,所以我添加了它,但现在我仅通过这一更改就遇到了段错误。

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

main(int argc, char *argv[]){

int numParts=10;
long numTests=49000;

char filename[] = "sweep_data.txt";
FILE *fp= fopen(filename,"r");
FILE *out= fopen("TesterData.csv", "w+");
FILE *meanf= fopen("meanData.txt", "w+");

char delims[] = " <>";
char *result = NULL;
char line [128];

char *TestNum=NULL;
char *TestName=NULL;
char *TestName2=NULL;
char *SequencerName=NULL;
char *lowlim=NULL;
char *hilim=NULL;
char *value=NULL;
char *units=NULL;
char *DeviceNum=NULL;

char ValueArray[numTests][10];
char ***ValuePtr = NULL;
char InfoArray[numTests][20];
char ***InfoPtr = NULL;

double mean[numTests];
long sum;
int intResult;
int count=0;
int DeviceCount=-1;
int i,j, m,n,k,a,b, len, mLen;

/*Allocate Memory for 2D arrays*/
ValuePtr = malloc(numParts * sizeof *ValuePtr);

for(i=0; i<numParts;i++){
ValuePtr[i]=malloc(numTests*sizeof *ValuePtr);


for(j=0; j<numTests; j++){
ValuePtr[i][j] = malloc(strlen(ValueArray[j]) +1);
}
}

InfoPtr = malloc(6 * sizeof *InfoPtr);

if(InfoPtr != NULL){
for(a=0; a<6;a++){
InfoPtr[a]=malloc(numTests*sizeof *InfoPtr);

for(b=0; b<numTests; b++){
InfoPtr[a][b]= malloc(strlen(InfoArray[b]) +1);
}
}
}

while(fgets(line, sizeof line, fp) != NULL){
result = strtok(line, delims);
TestNum=result;

intResult = strtol(result, NULL, 10);
if(intResult ==0){
if(strcmp(result, "Device:")==0){
DeviceCount++;
count=0;
}
continue; //if doesn't start with a number go to next line
}

result = strtok(NULL, delims);
TestName= result;
result = strtok(NULL, delims);
TestName2= result;
result = strtok(NULL, delims);
SequencerName = result;
lowlim=SequencerName;

if(atof(SequencerName)>1 || atof(SequencerName)<-1){
result= strtok(NULL, delims);
lowlim=result;
strcat(TestName, TestName2);
}
else
SequencerName= TestName2;


if(strstr(TestName, "%")!=NULL || strcmp(TestName,"PTgen")==0 || strcmp(TestName,"mode")==0){
units="NA";
}

else if(strstr(TestName, "2nd") == NULL && strstr(TestName, "3rd") == NULL){
result= strtok(NULL, delims);
units = result;
}

result= strtok(NULL, delims);
value=result;

if(strstr(TestName, "%")==NULL && strcmp(TestName,"PTgen")!=0 && strcmp(TestName,"mode")!=0){
result=strtok(NULL, delims);
if(strstr(TestName, "2nd") != NULL || strstr(TestName, "3rd") != NULL)
units = result;
}

result=strtok(NULL, delims);
hilim=result;
if(hilim[strlen(hilim)-1]=='\n'){
hilim[strlen(hilim)-1]='\0';
hilim[strlen(hilim)-1]='\0';
}

if(DeviceCount==0){
strcpy(InfoPtr[0][count], TestNum);
strcpy(InfoPtr[1][count], TestName);
strcpy(InfoPtr[2][count], SequencerName);
strcpy(InfoPtr[3][count], lowlim);
strcpy(InfoPtr[4][count], hilim);
strcpy(InfoPtr[5][count], units);
}

strcpy(ValuePtr[DeviceCount][count],value);

count++;
}

for(b=0;b<numTests;b++){
sum=0;
for(a=0;a<numParts;a++){
fprintf(meanf, "%s\n", ValuePtr[a][b]);
sum=atof(ValuePtr[a][b]);
fprintf(meanf, "%f\n",sum);
}
mean[b]=sum/numParts;
for(n=0; n<2;n++){
fprintf(meanf, "%s ", InfoPtr[n][b]);
}
fprintf(meanf, "%f\n",mean[b]);
}
printf("NumTests: %i\n",count); //number of tests run
printf("NumParts: %i\n", DeviceCount+1);//number of parts run
fprintf(out,"Test#, TestName,SeqName,LowLim,UpLim,Units,");

for(j=1; j<=numParts;j++){
fprintf(out," Device#%i,", j);
}
fprintf(out, "\n");

for (n = 0; n < numTests; n++) {
for(a=0;a<6;a++){
fprintf(out, "%s", InfoPtr[a][n]);

fprintf(out, ", ");
}

for (m =0; m < numParts ; m++) {
fprintf(out, "%s, ", ValuePtr[m][n]);
}
fprintf(out, "\n");
}
}

我需要转换为 double 的部分是

for(b=0;b<numTests;b++){
sum=0;
for(a=0;a<numParts;a++){
fprintf(meanf, "%s\n", ValuePtr[a][b]);
sum+=atof(ValuePtr[a][b]);
fprintf(meanf, "%f\n",sum);
}
mean[b]=sum/numTests;
for(n=0; n<2;n++){
fprintf(meanf, "%s ", InfoPtr[n][b]);
}
fprintf(meanf, "%f\n",mean[b]);
}

最佳答案

以下行似乎不正确。它正在对尚未初始化的内容使用 strlen。因此结果是不确定的:

ValuePtr[i][j] = malloc(strlen(ValueArray[j]) +1);

稍后在调用 atof() 时使用它很可能不会产生一致的结果。并且根据实际分配大小,在其中存储数据时可能会导致段错误。

关于c - 包含 <stdlib.h> 会导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3135828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com