线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)标准C内存问题

我正在编写代码来比较标准C中的两个输入文件,使用Xcode IDE。 我一直收到这个错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)。 我已经对此做了一些阅读,并认为这是一个内存问题,但无论我尝试什么,我似乎无法修复它(我也尝试使用malloc动态制作结构并列出底部的代码)。 这很奇怪,因为它会写入所有数据,然后在最后发出错误。 文件格式是这样的:start(int).. stop(int)id(+或 – )现在有些东西我不关心其余部分我刚刚在一个文件上测试这个只有+ id,所以“ – ”方面不是问题的一部分。 无论如何我已经累了,已经盯着这几个小时了,所以请原谅我,如果它没有意义,我会在几个小时的睡眠后更新它。

typedef struct { int start; int stop; char *strandID; } location; int main(int argc, const char * argv[]) { if (argc != 4) { fprintf(stderr, "Usage is ./a.out windowfile.txt genefile.txt outputFileName"); exit(-1); } //const vars const char *windowInput = argv[1]; const char *geneInput = argv[2]; const char *outputfile = argv[3]; const int windowHeader = 9; const int geneHeader = 3; //get size of structures -- I have debugged and these work correctly, returning the size of my structure const int posWsize = getSize(windowInput, "+", windowHeader); const int negWsize = getSize(windowInput, "-", windowHeader); const int posGsize = getSize(geneInput, "+", geneHeader); const int negGsize = getSize(geneInput, "-", geneHeader); //declare structs location posWindow[posWsize]; location negWindow[negWsize]; location posGene[posGsize]; location negGene[negGsize]; //extract data here getLocations(posWindow, negWindow, windowInput, windowHeader); return 0; } void getLocations(location *posL, location *negL, const char *input, const int header) { FILE *fileptr = NULL; fileptr = fopen(input, "r"); //open file if (fileptr == NULL) { //check for errors while opening fprintf(stderr, "Error reading %s\n", input); exit(-1); } char tmpLoc[20]; char tmpID[2]; int eofVar = 0; int lineCount = 0; while (lineCount 
start = start; tmp->stop = stop; tmp->strandID = (char *) malloc(sizeof(char) * 2); strcpy(tmp->strandID, strandID); return tmp; }

检查strtok的返回值。

在你的代码中

 locTok = strtok(NULL, ".."); posL[pCount].stop = atoi(locTok); //ERROR IS SHOWN HERE 

strtok返回一个NULL指针并根据文档 ,

如果没有要检索的标记,则返回空指针。

这符合我原来的猜测,因为地址代码是0x0 ,某处有一个NULL指针deference。

显然,以下对atoi调用期望非NULL指针和崩溃。

您应该删除主函数的参数。 它会工作。