NTL Library ref_GF2运行时错误

时间:2016-02-27 20:19:02

标签: c++ ntl

我正在使用NTL C ++库。在尝试执行以下代码时:

NTL::ref_GF2 *zero = new NTL::ref_GF2();
NTL::ref_GF2 *one = new NTL::ref_GF2();
set(*one);

我收到了EXC_BAD_INSTRUCTION错误:

ref_GF2 operator=(long a)
{
   unsigned long rval = a & 1;
   unsigned long lval = *_ref_GF2__ptr;
   lval = (lval & ~(1UL << _ref_GF2__pos)) | (rval << _ref_GF2__pos);
   *_ref_GF2__ptr = lval;
   return *this;
}

问题似乎源于set(* one)代码行。

我一直试图了解代码中出了什么问题,但无济于事。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

来自documentation

  

GF2的头文件也声明了类ref_GF2   用于表示非常量引用GF2,[...]。

     

ref_GF2const GF2的隐式转化   从GF2&ref_GF2

您收到错误是因为您正在定义没有目标的引用。 在您致电set(*one)时,*one未指向GF2,因此会引发错误。

如果您在致电GF2之前指向set(*one)

,它可以正常工作
NTL::GF2 x = GF2();
NTL::set(x);               // x = 1

NTL::ref_GF2 *zero = new NTL::ref_GF2(x);
NTL::ref_GF2 *one  = new NTL::ref_GF2(x);

// this works now
NTL::clear(*zero);
NTL::set(*one);

cout << *zero << endl;     // prints "1"
cout << *one << endl;      // prints "1"

请注意,ref_GF2表示对GF2的引用。我的示例代码显示零和一都指向x。也许您想使用GF2代替ref_GF2