Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
948 views
in Technique[技术] by (71.8m points)

xcode - Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) standard C memory issue

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
", 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 == '
')
      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=%dstop=%dID=%sindex=%d
", 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)) != '
')
    {
      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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...