C:中止陷阱6错误

时间:2017-09-25 12:34:56

标签: c

我正在尝试编译一个新的代码,在 calc 函数下,它一直给我“Abort Trap:6”错误。整个代码粘贴在底部,但是一些调试显示错误发生在以下第6行:

void calc(void){
bool found =false;
int x=0;
for(int i=0;i<500;i++){
    for(int ii=0;ii<=i;ii++){
        if(logarray[1][i]==newlog[0][ii]){
        found=true;
        break;
        }
    }

似乎在尝试写入数组分配之外的内存时会发生这种情况,但我似乎无法弄清楚为什么在这种情况下会导致问题。

完整代码:

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

#define Packets "/Users/James/Google Drive/University/CITS2002/Project 1/sample-packets.txt" 

char logarray[4][500][1028];
char newlog[2][500][1028];
//--------------------------------------------------------------------------
void calc(void){
bool found =false;
int x=0;
for(int i=0;i<500;i++){
    for(int ii=0;ii<=i;ii++){
        if(logarray[1][i]==newlog[0][ii]){
        found=true;
        break;
        }
    }
    if (found==false){
    int total=0;
    strcpy(newlog[0][x],logarray[1][i]);
        for(int y=0;y<500;y++){
            if(logarray[1][y]==logarray[1][i]){
            total=total+atoi(logarray[3][y]);
            }
        }

    char str[2];
    sprintf(str, "%d", total);
    strcpy(newlog[1][x],str);
    x++;
    }
}

for (int t=0;t<10;t++){
                for (int p=0;p<2;p++){


                        printf("%s \t", newlog[p][t]);
                        }
                printf("\n");
                }

}
//---------------------------------------------------------------------------
void read(int ROT){

char currentline[128];
char *token;

FILE *log;
log= fopen(Packets,"r");

    if(log==NULL){
        printf("Cannot Open\n");
        exit(EXIT_FAILURE);
    }

int x =0;
int y =0;

while (fgets(currentline, 1024,log) !=NULL){
    fgets(currentline,1024,log);
    printf("%s \n", currentline);
    token = strtok(currentline,"\t");
    strcpy(logarray[x][y],token);

        for(int i=0;i<3;i++){
        x++;
            if(x>3){x=0;}

        token=strtok(NULL, "\t");
        strcpy(logarray[x][y],token);   

        }

    y++;
    x=0;    
    }
printf("logarray:\n");  
for (int t=0;t<10;t++){
        for (int p=0;p<4;p++){


            printf("%s \t", logarray[p][t]);
            }
        printf("\n");   
        }

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

if (argc < 3 || argc > 4){

    printf("Error, incorrect number of arguments");
    exit(EXIT_FAILURE);
    }

if (argv[1][0]=='t'){
    read(2);
    }

else if (argv[1][0]=='r'){
    read(3);
    }

else    {
    printf("Error, please specify either 'r' or 't'");
    exit(EXIT_FAILURE);
    }   
calc();

return 0;

}

0 个答案:

没有答案