为什么我得到一个"分段错误"错误,当我尝试运行测试? [英] Why am I getting a "Segmentation Fault" error when I try to run the tests?

查看:98
本文介绍了为什么我得到一个"分段错误"错误,当我尝试运行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了决定是否指定默认值的功能(如果标志不是present,而且如果标志为present用户通过赋值它分配默认值)。而且我想测试我的函数的字符串,看它是否确实给了我正确的数字。我不断收到段错误当我尝试运行测试,它编译,但测试只是不工作。 :(

下面是我的头文件:

 的#ifndef COMMANDLINE_H
#定义COMMANDLINE_H
#包括data.h
#包括LT&;&stdio.h中GT;结构点睛;/ *上面列出的变量是一个全局变量* /无效eye_flag(INT arg_list,字符*数组[]);#万一

下面是我的实现文件:

 的#include<&stdio.h中GT;
#包括commandline.h
#包括data.h
的#includestring.h中对于结构点眼/ *使用的全局变量* /无效eye_flag(INT arg_list,字符* []数组)
{
   eye.x = 0.0;
   eye.y = 0.0;
   eye.z = -14.0;   / *的结构点眼上面列出的值是缺省值。 * /   的for(int i = 0; I< = arg_list;我++)
   {
      如果(STRCMP(数组[我],-Eye)== 0)
      {
         sscanf的(数组[I + 1],%LF,&安培; eye.x);
         sscanf的(数组第[i + 2],%LF,&安培; eye.y);
         sscanf的(数组[I + 3],%LF,&安培; eye.z);
      }
   }
}

这是我的测试用例:

 的#includecommandline.h
#包括checkit.h
#包括LT&;&stdio.h中GT;无效eye_tests(无效)
{
   字符* 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);   字符* 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);
}诠释的main()
{
   eye_tests();   返回0;
}


解决方案

的错误是在这里:

 的for(int i = 0; I< = arg_list;我++)
  {/// ^^
      如果(STRCMP(数组[我],-Eye)== 0)
      {
          sscanf的(数组[I + 1],%LF,&安培; eye.x);
                   // ^^^
          sscanf的(数组第[i + 2],%LF,&安培; eye.y);
          sscanf的(数组[I + 3],%LF,&安培; eye.z);
      }
  }


  1. I< = arg_list 是错误的,因为你在6传递,数组索引从0开始,最大值为5

  2. I + 1,I + 2,I + 3 当你从0迭代到5给你出界指数。

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 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.

这篇关于为什么我得到一个&QUOT;分段错误&QUOT;错误,当我尝试运行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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