简体   繁体   中英

Why am I getting a “Segmentation Fault” error when I try to run the tests?

I've written a function that determines whether or not to assign default values (it assigns default values if the flag is not present, and it assigns values the user passes if the flag is present). And I'm trying to test my function with a string to see if it did give me the right numbers. I keep getting "Segmentation Fault" when I try to run the tests, it compiles, but the tests just don't work. :(

Here's my header file:

#ifndef COMMANDLINE_H
#define COMMANDLINE_H
#include "data.h"
#include <stdio.h>

struct point eye;

/* The variable listed above is a global variable */

void eye_flag(int arg_list, char *array[]);

#endif

Here's my implementation file:

#include <stdio.h>
#include "commandline.h"
#include "data.h"
#include "string.h"

/* Used global variables for struct point eye */

void eye_flag(int arg_list, char *array[])
{
   eye.x = 0.0;
   eye.y = 0.0;
   eye.z = -14.0;

   /* The values listed above for struct point eye are the default values. */

   for (int i = 0; i <= arg_list; i++)
   {
      if (strcmp(array[i], "-eye") == 0)
      {
         sscanf(array[i+1], "%lf", &eye.x);
         sscanf(array[i+2], "%lf", &eye.y);
         sscanf(array[i+3], "%lf", &eye.z);
      }
   }
}

And here are my test cases:

#include "commandline.h"
#include "checkit.h"
#include <stdio.h>

void eye_tests(void)
{
   char *arg_eye[6] = {"a.out", "sphere.in.txt", "-eye", "2.4", "3.5", "6.7"};
   eye_flag(6, arg_eye);

   checkit_double(eye.x, 2.4);
   checkit_double(eye.y, 3.5);
   checkit_double(eye.z, 6.7);

   char *arg_eye2[2] = {"a.out", "sphere.in.txt"};
   eye_flag(2, arg_eye2);

   checkit_double(eye.x, 0.0);
   checkit_double(eye.y, 0.0);
   checkit_double(eye.z, -14.0);
}

int main()
{
   eye_tests();

   return 0;
}

The absolute easiest way to solve this one is run it in a debugger. You probably won't even need to learn how to step through your code or anything - just fire up, run, and read the line.

If you are on a *nix system:

  1. Compile your code with -g flag.
  2. Load as, eg gdb a.out .
  3. Run now that it's loaded - (gdb) run .
  4. Do whatever you need to reproduce the segfault.
  5. bt or where should give you a stack trace - and an exact line that is causing your problem.

I'm sure enough you can solve it from there to post this as an answer; but if not, knowing the exact line will make it very much easier to research and solve.

Your loop condition is wrong. It should be i < arg_list .
Think about what happens when i == arg_list .

The errors are here:

  for (int i = 0; i <= arg_list; i++)
  {            ///^^
      if (strcmp(array[i], "-eye") == 0)
      {
          sscanf(array[i+1], "%lf", &eye.x);
                   //^^^
          sscanf(array[i+2], "%lf", &eye.y);
          sscanf(array[i+3], "%lf", &eye.z);
      }
  }
  1. i <= arg_list is wrong since you pass in 6, array index starts from 0, the max value is 5
  2. i+1, i+2,i+3 will give you out of bounds index when you iterate from 0 to 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM