简体   繁体   English

NTL库ref_GF2运行时错误

[英]NTL Library ref_GF2 Run-time Error

I am using the NTL C++ Library. 我正在使用NTL C ++库。 On trying to execute the following code: 在尝试执行以下代码时:

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

I am getting an EXC_BAD_INSTRUCTION error: 我收到一个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;
}

The problem seems to stem from the set(*one) line of code. 问题似乎源于代码的set(* one)行。

I've been trying to understand what's going wrong in the code, to no avail. 我一直试图理解代码中出了什么问题,但没有成功。 Any help appreciated. 任何帮助赞赏。

From the documentation : 文档

The header file for GF2 also declares the class ref_GF2 , which use used to represent non-const references to GF2 's, [...]. GF2的头文件还声明了类ref_GF2 ,该类用于表示对GF2非常量引用 。[...]

There are implicit conversions from ref_GF2 to const GF2 and from GF2& to ref_GF2 . ref_GF2const GF2以及从GF2&ref_GF2都有隐式转换。

You get the error because your are defining a reference without a target. 因为您正在定义没有目标的引用,所以会出现错误。 At the point where you call set(*one) , *one does not point to a GF2 and so it throws an error. 在您调用set(*one)*one并不指向GF2 ,因此会引发错误。

It works fine, if you point to a GF2 before calling set(*one) : 如果在调用set(*one)之前指向GF2 ,它会很好地工作:

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"

Notice that ref_GF2 represents a reference to a GF2 . 注意, ref_GF2表示对GF2的引用。 My example code shows that zero and one both point to x . 我的示例代码显示0和1都指向x Maybe you want to use GF2 instead of ref_GF2 . 也许您想使用GF2而不是ref_GF2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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