加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 01:47:47 所属栏目:百科 来源:网络整理
导读:我正在编写代码来比较标准C中的两个输入文件,使用Xcode IDE.我一直收到这个错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0).我已经对此做了一些阅读,并认为这是一个内存问题,但无论我尝试什么,我似乎无法修复它(我也尝试使用malloc动态制作结构并列出底部的
我正在编写代码来比较标准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 %sn",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=%dtstop=%dtID=%stindex=%dn",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;
}

解决方法

检查strtok的返回值.

在你的代码中

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

strtok返回一个NULL指针,根据documentation,

A null pointer is returned if there are no tokens left to retrieve.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读