“Abort trap:6”在Mac上运行C程序

我有这个程序的问题。 当我尝试在Mac上运行它时,它会返回“Abort trap:6”。 我无法理解原因。

下面是我用来测试程序的文件内容。

aaabccdegags
bbbbbbbcados
vbaiusbyabtd
aisybavsitvc
asindvsivati
ammaccabanan

您应该将这些无意义的字符串复制到文本文件file.txt并使用以下语法通过命令提示符调用它:

program.exe file.txt r

其中file.txt包含无意义字符串, r是它应在文本文件中查找的字母。

 #include  #include  #include  #include  #define R 24 #define C 71 int main(int argc, char * argv[]) { if (argc!=3){ printf("Errore acquisizione parametri\n"); return -1; } FILE*fp; char m[R][C],c,posizione[10]; int i,j,lstr,numrighe,quante,quantemax,posiz,x,y,maxmax; maxmax=0; //posiz=1 se orizzontale quantemax=0; sscanf(argv[2],"%c",&c); fp=fopen(argv[1],"r"); if (fp==NULL){ printf("Errore apertura file\n"); return -3; } for (i=0;i<R && (!feof(fp));i++){ fgets(m[i],C,fp); } fclose(fp); lstr=strlen(m[0]); numrighe=i; for (i=0;i<lstr;i++){ quante=0; for (j=0;jquantemax){ quantemax=quante; if (quantemax>maxmax){ y=i; x=j; posiz=1; } } } } } for (i=0;i<lstr;i++){ quante=0; for (j=0;jquantemax){ quantemax=quante; if (quantemax>maxmax){ y=i; x=j; posiz=0; } } } } } if (posiz==1){ strcpy(posizione,"orizzontale"); } else { if (posiz==0){ strcpy(posizione,"verticale"); } } printf("La stringa con il maggior numero di %c consecutive, si trova in %sa partire dalla posizione %d,%d",c,posizione,x,y); return 0; } 

我没有使用给定的输入运行您的代码,但没有它,看起来,您在代码中面临UB。 在声明的情况下

  strcpy(posizione,"orizzontale"); 

源字符串的长度超过目标可以容纳的长度。 它尝试写入已分配的内存,然后调用未定义的行为 。