主题1:EXC_BAD_ACCESS(code = 1,地址=为0x0)标准C内存问题 [英] Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) standard C memory issue

查看:2251
本文介绍了主题1:EXC_BAD_ACCESS(code = 1,地址=为0x0)标准C内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写code在标准C两个输入文件进行比较,使用X code IDE。我不断收到此错误:线程1:EXC_BAD_ACCESS(code = 1,地址=为0x0)。我已经做了这方面的一些阅读,并认为它是内存问题,但不管是什么似乎我尝试,我不能修复它(我也试图使结构动态使用malloc和上市,在底部在code)。这很奇怪,因为它写的所有数据,然后在最后吐出的错误。文件格式是这样的:
启动(INT)..停止(INT)ID(+或 - )现在有些东西我不关心的行的其余部分
我刚刚上的一个文件,只有+ ID的测试此所以 - 问题的一个方面是不是一部分。反正我很疲惫,并在此一直盯着几个小时,所以请原谅我,如果它没有任何意义,我会睡几个小时后更新。

  typedef结构
{
  INT开始;
  INT停止;
  字符* strandID;
} 位置;INT主(INT ARGC,为const char * argv的[])
{
  如果(argc个!= 4)
  {
    fprintf中(标准错误,
        用法是./a.out windowfile.txt genefile.txt outputFileName);
    出口(-1);
  }  //常量瓦尔
  为const char * windowInput =的argv [1];
  为const char * geneInput =的argv [2];
  为const char * OUTPUTFILE =的argv [3];  const int的windowHeader = 9;
  const int的geneHeader = 3;  //获取结构的大小 - 我已经调试这些正常工作,给我回结构的大小
  const int的posWsize =的getSize(windowInput,+,windowHeader);
  const int的negWsize =的getSize(windowInput, - ,windowHeader);
  const int的posGsize =的getSize(geneInput,+,geneHeader);
  const int的negGsize =的getSize(geneInput, - ,geneHeader);  //声明结构
  位置posWindow [posWsize]
  位置negWindow [negWsize]
  位置posGene [posGsize]
  位置negGene [negGsize]  //此处提取数据
  的getLocations(posWindow,negWindow,windowInput,windowHeader);
  返回0;
}无效的getLocations(位置* posL,位置* negL,为const char *输入,
    const int的头)
{
  FILE * fileptr = NULL;
  fileptr = FOPEN(输入R); //打开文件  如果(fileptr == NULL)
  {//检查错误,同时打开
    fprintf中(标准错误,错误读取%S \\ n,输入);
    出口(-1);
  }  炭tmpLoc [20];
  炭tmpID [2];
  INT eofVar = 0;
  INT lineCount = 0;  而(lineCount<头)
  {//跳过头,并获得数据
    eofVar =龟etc(fileptr);
    如果(eofVar =='\\ n')
      lineCount ++;
  }  INT pCount = 0;
  INT NCOUNT = 0;  而(eofVar!= EOF)
  {
    的fscanf(fileptr,%s%S,tmpLoc,tmpID); //扫描前两个字符串
    如果(!的strcmp(tmpID,+))
    {//如果+链
      字符* locTok = NULL;
      locTok = strtok的(tmpLoc,..); // TOK并获得价值
      posL [pCount]。开始=的atoi(locTok);
      locTok =的strtok(NULL,..);
      posL [pCount] .stop =的atoi(locTok); //错误显示HERE      posL [pCount] .strandID = tmpID;
      的printf(开始=%d个\\ TSTOP =%d个\\ TID =%S \\ tindex =%d个\\ N,posL [pCount]。开始,
          posL [pCount] .stop,posL [pCount] .strandID,pCount);
      pCount ++;
    }
    否则如果(的strcmp(tmpID, - ))
    {//如果 - 链
      字符* locTok = NULL;
      locTok = strtok的(tmpLoc,..); // TOK并获得价值
      negL [NCOUNT]。开始=的atoi(locTok);
      locTok =的strtok(NULL,..);
      negL [NCOUNT] .stop =的atoi(locTok);      negL [NCOUNT] .strandID = tmpID;
      NCOUNT ++;
    }    而((eofVar =龟etc(fileptr))!='\\ n')
    {
      如果(eofVar == EOF)
        打破;
    }
  }  FCLOSE(fileptr);
}//动态的方式...同样的问题 - 只是上面的if语句替换此并使用创建定位功能
如果(!的strcmp(tmpID,+))
{//如果+链
  INT locStart;
  INT locStop;  locStart =的atoi(strtok的(tmpLoc,..)); //托克和获取值
  locStop =的atoi(的strtok(NULL,));  posL [pCount] = * createlocation(locStart,locStop,tmpID);  pCount ++;
}位置* createlocation(INT开始,诠释停止,字符* strandID)
{
  位置* TMP = NULL;
  TMP =(位置*)malloc的(的sizeof(位置)* 1);  tmp->启动=启动;
  tmp->停止=停止;
  tmp-> strandID =(字符*)malloc的(的sizeof(字符)* 2);
  的strcpy(tmp-> strandID,strandID);  返回TMP;
}


解决方案

检查返回值 strtok的

在您的code此处

  locTok =的strtok(NULL,..);
posL [pCount] .stop =的atoi(locTok); //错误显示HERE

strtok的将返回NULL指针,并根据的文档


  

如果没有留下记号检索空指针被返回。


这我原来的猜测匹配,因为地址code是为0x0 有一个NULL指针尊重的地方。

显然,下面的调用的atoi 期待一个非NULL指针和崩溃。

I'm writing code to compare two input files in standard C, using the Xcode IDE. I keep getting this error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0). I've done some reading on this and believe it to be a memory issue, but no matter what I try I can't seem to fix it (I've also tried making the structures dynamically using malloc and listed that at the bottom of the code). It's strange because it writes all of the data and then spits out that error at the end. The file format is something like this: start(int)..stop(int) id(+ or -) now some stuff I don't care about for the rest of the line I've just been testing this on a file with only + id's so the "-" aspect isn't part of the issue. Anyway I'm quite tired and have been staring at this for a few hours, so please forgive me if it doesn't make sense, I will update it after a few hours of sleep.

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;
}

解决方案

Check the return value of strtok.

In your code here

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

strtok is returning a NULL pointer and according to documentation,

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

which matches my original guess that because the address code is 0x0 there's a NULL pointer deference somewhere.

Obviously, the following call to atoi is expecting a non-NULL pointer and crashes.

这篇关于主题1:EXC_BAD_ACCESS(code = 1,地址=为0x0)标准C内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆