Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Error: thread 1: exc_bad_access (code=1, address=0x0) – quick tips
Next
Answered
Lucian Rodriguez
  • 16
Lucian Rodriguez
Asked: May 18, 20222022-05-18T20:17:45+00:00 2022-05-18T20:17:45+00:00In: c

Error: thread 1: exc_bad_access (code=1, address=0x0) – quick tips

  • 16

. Advertisement .

..3..

. Advertisement .

..4..

I get the “thread 1: exc_bad_access (code=1, address=0x0)” error as the title says. How can I fix it so the error goes away? Here is my detail:

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

When I operated it, I received the error text:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

I appreciate any help from you.

thread 1: exc_bad_access
  • 2 2 Answers
  • 341 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-29T14:57:15+00:00Added an answer on June 29, 2022 at 2:57 pm

    The cause:

    I find that in your program:

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

    Base on this documentation, strtok is giving back a NULL pointer.

    If no tokens are retrieved, a null pointer is given back. It’s correct with my initial hypothesis that there is a NULL pointer deference because the address code is 0×0. However, the subsequent atoi call fails because it expects a non-NULL pointer, so the error happens.

    Solution:

    Exception Breakpoint in Xcode can help you resolve this error: First, let’s access to the Breakpoint Navigation, after that select Add Exception Breakpoint by clicking the Plus button in the bottom left corner.

    Another way is for xc method:

    Let’s attempt to exclude char*argv[] or both arguments in your main() function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Jasper Thomas
    2022-05-26T04:10:25+00:00Added an answer on May 26, 2022 at 4:10 am


    The Exception Breakpoint can also be used in Xcode.

    An exception breakpoint tells the debugger to pause whenever a problem is encountered anywhere in your program, so you can evaluate your program’s state before it crashes.

    Navigate to Breakpoint Navigation (Cmd+8). Click the + button at the bottom left, and select Add Exception Breakpoint. You can also leave it as is.


    thread%201:%20exc_bad_access%20(code=1,%20address=0x0)

    • 19
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.