段错误双指针

segmentation fault double pointer

我在以下代码中遇到分段错误,我真的没有看到我错过了什么,这段代码的目的是检索 .csv 的行并将它们放入使用双指针.

段错误位于 csv 文件第一行的明文 [i][j] 的最后分配处。

非常感谢您的帮助(关于这个问题从昨天开始...)

int main(){


int n=48; //nbers of columns in csv file
int m=60; //nbers of lines in csv file


int cpt,i,j;
cpt=0;
i=0;
FILE *fp;
char *token;
const char s[2] = ",";

unsigned char **plaintexts;
plaintexts = malloc(sizeof(*plaintexts) * m);

char *str=malloc(sizeof(char)*15*n); //maximum 15 char per box
fp = fopen("aes_traces.csv","r");




while(fgets(str,15*n,fp)!=NULL){

    plaintexts[i] = malloc(sizeof(*plaintexts[i]) * n);
    token = strtok(str,s);
    j=0;
    while(token != NULL){
        printf("%s\n", token);
        token = strtok(NULL,s);
        plaintexts[i][j]=(unsigned char) (*token);

        j++;

    }

    i++;
    free(str);
    free(token);
}


fclose(fp);

}
while(token != NULL){
    printf("%s\n", token);
    token = strtok(NULL,s); // A
    plaintexts[i][j]=(unsigned char) (*token); // B

    j++;
}

如果这个循环至少运行一次,它将以段错误结束。为什么?直到 token 在我标记为 A 的行中设置为 NULL,然后在我标记为 B 的行中取消引用,循环才能终止。取消引用 NULL 将导致段错误。