未初始化的值是由堆栈分配,分段错误创建的

时间:2017-11-15 21:50:45

标签: c++ binary-search

我正在编写以执行和测试二进制搜索。目标是当函数y = x-1的y <= 0时得到最大x,搜索范围是[-2,2],显然答案应该是1。 对于测试部分,我应该测试函数f的调用是否小于计算的max_invoke 出现了一个分段错误,当我对它进行valgrind时,错误是:

    ==126667== Use of uninitialised value of size 8
    ==126667==    at 0x400C8A: binarySearchForZero(Function<int, int>*, int, 
    int) (in /home/jw562/ece551/092_tests_binsrch/test)
    ==126667==    by 0x400DD5: check(Function<int, int>*, int, int, int, 
    char const*) (in /home/jw562/ece551/092_tests_binsrch/test)
    ==126667==    by 0x400E81: main (in 
    /home/jw562/ece551/092_tests_binsrch/test)
    ==126667==  Uninitialised value was created by a stack allocation
    ==126667==    at 0x400E41: main (in 
    /home/jw562/ece551/092_tests_binsrch/test)

下面是我的代码:

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath> 

    using namespace std;

    template<typename R, typename A> 
    class Function {
    public:
      virtual R invoke(A arg) = 0;
      virtual ~Function() {}
    };

    class CountedIntFn : public Function<int,int>{
    protected:
      unsigned remaining;
      Function<int,int> * f;
      const char * mesg;
    public:
      CountedIntFn(unsigned n, Function<int,int> * fn, const char * m): 
      remaining(n),f(fn),mesg(m) {}
      virtual int invoke(int arg) {
        if (remaining == 0) {
          fprintf(stderr,"Too many function invocations in %s\n", mesg);
          exit(EXIT_FAILURE);
      }
      remaining--;
      return f->invoke(arg);
    }

    class linearFunction : public Function<int, int> {
      public:
      virtual int invoke(int arg) {
        int ans = arg-1;
        return ans;
      }
      virtual ~linearFunction(){}
    };

    int binarySearchForZero(Function<int, int> * f, int low, int high){
        if (high <= low){
            cout << "high less or equal than low" << endl;
            return EXIT_FAILURE;
        }
        int low_ans = f->invoke(low);
        int high_ans = f->invoke(high);
        int mid = (high-low)/2+low;
        int mid_ans = f->invoke(mid);
        if (low_ans > 0){
             return low;//all positive
        }
        if (high_ans < 0){
             return high-1;//all negtive
        }
        if (low_ans = high_ans){
             return low;//all zero
        }
        if (mid_ans <= 0){
             low = mid;
        }
        if (mid_ans >= 0){
             high = mid;
        }
        return binarySearchForZero(f,low,high);
      }

      void check(Function<int,int> * f,int low,int high,int 
      expected_ans,const char * mesg){
        int max_invoke = 0;//the maximum number of invocations allowed
        if (high > low){
           max_invoke = (int)(log2(high-low))+1;
        }
        else {
           max_invoke = 1;
        }  
        CountedIntFn count(max_invoke,f,mesg);
        int ans = binarySearchForZero(f,low,high);
        if (ans != expected_ans){
           cout << "wrong answer" <<endl;
        }
      }

      int main(void){
         linearFunction *fl;
         const char * message = "linearFunction";
         int low = -2;
         int high = 2;
         int expected_ans = 1;
         check(fl,low,high,expected_ans,message);
         return EXIT_SUCCESS;
      }

1 个答案:

答案 0 :(得分:0)

这是因为fl类型的变量linearFunction*未初始化。

您可以将其更改为非指针,并使用address-of运算符调用:

linearFunction fl;
...
check(&fl, low, high, expected_ans, message);