iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)标准C内存问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在编写代码来比较标准C中的两个输入文件,使用Xcode IDE.我一直收到这个错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0).我已经对此做了一些阅读,并认为这是一个内存问题,但无论我尝试什么,我似乎无法修复它(我也尝试使用malloc动态制作结构并列出底部的代码).这很奇怪,因为它会写入所有数据,然后在最后发出错误.文件格式如下: start(int).. stop(
我正在编写@L_616_0@来比较标准C中的两个输入文件,使用Xcode IDE.我一直收到这个错误:线程1:EXC_BAD_ACCESS(@L_616_0@= 1,地址= 0x0).我已经对此做了一些阅读,并认为这是一个内存问题,但无论我尝试什么,我似乎无法修复它(我也尝试使用malloc动态制作结构并列出底部的@L_616_0@).这很奇怪,因为它会写入所有数据,然后在最后发出错误.文件格式如下:
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;
}

解决方法

检查strtok的返回值.

你的@L_616_0@中

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

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

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

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

大佬总结

以上是大佬教程为你收集整理的线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)标准C内存问题全部内容,希望文章能够帮你解决线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)标准C内存问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。