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

zeu*_*unk 9 c xcode memory-management

我正在编写代码来比较标准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 < header)
  { //skip header and get to data
    eofVar = fgetc(fileptr);
    if (eofVar == '\n')
      lineCount++;
  }

  int pCount = 0;
  int nCount = 0;

  while (eofVar != EOF)
  {
    fscanf(fileptr, "%s %s", tmpLoc, tmpID); //scan in first two strings
    if (!strcmp(tmpID, "+"))
    { //if + strand
      char *locTok = NULL;
      locTok = strtok(tmpLoc, ".."); //tok and get values
      posL[pCount].start = atoi(locTok);
      locTok = strtok(NULL, "..");
      posL[pCount].stop = atoi(locTok); //ERROR IS SHOWN HERE

      posL[pCount].strandID = tmpID;
      printf("start=%d\tstop=%d\tID=%s\tindex=%d\n", posL[pCount].start,
          posL[pCount].stop, posL[pCount].strandID, pCount);
      pCount++;
    }
    else if (!strcmp(tmpID, "-"))
    { //if - strand
      char *locTok = NULL;
      locTok = strtok(tmpLoc, ".."); //tok and get values
      negL[nCount].start = atoi(locTok);
      locTok = strtok(NULL, "..");
      negL[nCount].stop = atoi(locTok);

      negL[nCount].strandID = tmpID;
      nCount++;
    }

    while ((eofVar = fgetc(fileptr)) != '\n')
    {
      if (eofVar == EOF)
        break;
    }
  }

  fclose(fileptr);
}

//dynamic way...same issue -- just replace this with the above if statement and use the create location function
if (!strcmp(tmpID, "+"))
{ //if + strand
  int locStart;
  int locStop;

  locStart = atoi(strtok(tmpLoc, ".."));//tok and get values
  locStop = atoi(strtok(NULL, ".."));

  posL[pCount] = *createlocation(locStart, locStop, tmpID);

  pCount++;
}

location *createlocation(int start, int stop, char *strandID)
{
  location *tmp = NULL;
  tmp = (location *) malloc(sizeof(location) * 1);

  tmp->start = start;
  tmp->stop = stop;
  tmp->strandID = (char *) malloc(sizeof(char) * 2);
  strcpy(tmp->strandID, strandID);

  return tmp;
}
Run Code Online (Sandbox Code Playgroud)

tan*_*grs 5

检查 的返回值strtok

在您的代码中

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

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

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

这与我最初的猜测相符,因为地址代码是0x0某处存在 NULL 指针引用。

显然,以下对 的调用atoi期望非空指针并崩溃。