使用C中的动态bitset的后段故障++ [英] Segmentation fault after using dynamic bitset in C++

查看:180
本文介绍了使用C中的动态bitset的后段故障++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个可变长度的线性反馈移位寄存器,所以我用动态的bitset而不是从Boost库,而不是位集。编译程序,并运行时后,就产生分段故障后直接它显示xorArray的内容。我认为错误是在动态的bitset变量的定义,但我不能弄明白。有三个变量,inpSeq,operSeq和位。这里是code:

 的#include<&iostream的GT; //标准库。
#包括LT&;升压/ dynamic_bitset.hpp> //库10的处理。
#包括LT&;矢量> //可变大小的数组。
#包括LT&;&算法GT; //我们使用它的排序。使用命名空间std;诠释的main()
{
 INT Y = 0;
 INT turnCount = 0;
 INT的count1 = 0,COUNT0 = 0;
 提高::来,dynamic_bitset<> inpSeq;
 INT polyLoc;
 提高::来,dynamic_bitset<> operSeq;
 提高::来,dynamic_bitset<>位;
 矢量<诠释> xorArray;
 矢量<诠释> keyReg;
 COUT<< 输入一个位序列:\\ n;
 CIN>> inpSeq;
 INT seq_end = inpSeq.size() - 1;
 COUT<< 输入多项式:
 CIN>> polyLoc;
 而(polyLoc大于0)
 {
  xorArray.push_back(polyLoc%10);
  polyLoc / = 10;
 }
 COUT<< xorArray是:;
 为(unsigned int类型I = 0; I< xorArray.size();我++)
 {
  COUT<< xorArray [1] - ;&下; ;
 }
 排序(xorArray.rbegin(),xorArray.rend());
 COUT<< \\ n;
 operSeq = inpSeq;
 keyReg.push_back(inpSeq [0]);
  INT X = xorArray [0];
  COUT<< x是:&所述;&下;点¯x所述&;&下; \\ n;
  为(unsigned int类型I = 0; I< xorArray.size();我++)
  {
   COUT<< xorArray [1] - ;&下; \\ n;
  }
  COUT<< 位的最初3<<位[seq_end<< \\ n;
  做{
  对(无符号整数R = 1;为r xorArray.size(); R ++)
  {
  位[seq_end] = operSeq [X];
  COUT<< 3位从preV:<<位[seq_end<< \\ n;
  Y = xorArray [R]。
// COUT<< opseq [Y]是:<< operSeq [Y]<< \\ n;
  位[seq_end] =位[seq_end] ^ operSeq [Y];
// COUT<< 位[3] XOR之后:&所述;&下;位[seq_end<< \\ n;
  }
  operSeq>> = 1;
// COUT<<后移operSeq:<< operSeq<< \\ n;
  operSeq [seq_end] =位[seq_end]
// COUT<<opserSeq =位[3]之后的第4位:&LT< operSeq [seq_end<< \\ n;
// COUT&所述;&下,新operSeq:&所述;&下; operSeq<< \\ n;
  keyReg.push_back(operSeq [0]);
  turnCount ++;
  COUT<< - \\ n;
 }
 而((operSeq = inpSeq)及!及(turnCount&小于20));
 COUT<< 生成的关键是:;
 对(无符号整数K = 0; K< keyReg.size(); K ++)
  {
  COUT<< keyReg [K];
  }
 COUT<< \\ n;
 COUT<< 第1位的位置:;
 为(unsigned int类型G = 0; G< xorArray.size(); G ++)
 {
  COUT<< xorArray并[g];
 }
 COUT<< \\ n;
 COUT<< 密钥长度为:<< keyReg.size();
 COUT<< \\ n;
 为(unsigned int类型I = 0; I< keyReg.size();我++)
 {
  如果(keyReg [I] == 1)
   {
    COUNT1 ++;
   }
  其他{
    COUNT0 ++;
  }
 }
 COUT<< 0数:&所述;&下; COUNT0<< \\ n;
 COUT<< 1号:&所述;&下; COUNT1<< \\ n;
 如果(keyReg.size()%2 == 0)
  {
   COUT<< 密钥长度为偶数\\ n;
   如果(COUNT1 == COUNT0)
    {
   COUT<< 关键是完美的\\ N!
    }
  其他{
   COUT<< 关键是不完美的\\ N!
    }
 }
  其他
   {
  COUT<< 密钥长度为奇数\\ n;
   如果((COUNT1 == COUNT0 + 1)||(COUNT0 == COUNT1 + 1))
    {
   COUT<< 关键是完美的\\ N!
    }
  其他{
   COUT<< 关键是不完美的\\ N!
    }
   }
  cin.get();
}


解决方案

一个问题是,你没有规模你使用它之前dynamic_bitsets。你应该叫 来,dynamic_bitset ::调整大小

I am trying to make a variable length linear feedback shift register, so I used dynamic bitset instead from boost library instead of bitset. After compiling the program, and when running it, it produced Segmentation fault directly after it shows the contents of xorArray. I think the mistake is in the definition of the dynamic bitset variables, but I can't figure it out. There are three variables, inpSeq, operSeq and bit. Here is the code:

#include <iostream>  //Standard library.
#include <boost/dynamic_bitset.hpp>    //Library for 10 handling.
#include <vector>    //Variable size array.
#include <algorithm> //We use sorting from it.

using namespace std;

int main()
{
 int y = 0;
 int turnCount = 0;
 int count1 = 0, count0 = 0;
 boost::dynamic_bitset<> inpSeq;
 int polyLoc;
 boost::dynamic_bitset<> operSeq;
 boost::dynamic_bitset<> bit;
 vector <int> xorArray;
 vector <int> keyReg;
 cout << "Enter a bit sequence: \n";
 cin >> inpSeq;
 int seq_end = inpSeq.size() - 1;
 cout << "Enter polynomial:";
 cin >> polyLoc;
 while(polyLoc>0)
 {
  xorArray.push_back(polyLoc%10);
  polyLoc/=10;
 }
 cout << "xorArray is: ";
 for ( unsigned int i = 0; i < xorArray.size(); i++)
 {
  cout << xorArray[i] << " ";
 }
 sort(xorArray.rbegin(), xorArray.rend());
 cout << "\n";
 operSeq = inpSeq;
 keyReg.push_back(inpSeq[0]);
  int x = xorArray[0];
  cout << "x is: " << x << "\n";
  for ( unsigned int  i = 0; i < xorArray.size();  i++)
  {
   cout << xorArray[i] << "\n";
  }
  cout << "bit 3 of initial " << bit[seq_end] << "\n";
  do {
  for (unsigned int r = 1; r < xorArray.size(); r++)
  {
  bit[seq_end] = operSeq[x];
  cout << "bit 3 from prev: " << bit[seq_end] << "\n";
  y = xorArray[r];
//  cout << "opseq[y] is: " << operSeq[y] << "\n";
  bit[seq_end] = bit[seq_end] ^ operSeq[y];
//  cout << "bit[3] after xor: " << bit[seq_end] << "\n";
  }
  operSeq >>= 1;
//  cout <<"operSeq after shift: " <<  operSeq << "\n";
  operSeq[seq_end]  = bit[seq_end];
//  cout <<"opserSeq bit 4 after = bit[3]: " << operSeq[seq_end] << "\n";
//  cout <<"new operSeq: " << operSeq << "\n";
  keyReg.push_back(operSeq[0]);
  turnCount ++;
  cout << "--\n";
 }
 while ((operSeq != inpSeq) && (turnCount < 20));
 cout << "Generated key is: ";
 for (unsigned int k = 0; k < keyReg.size(); k++)
  {
  cout  <<  keyReg[k];
  }
 cout << "\n";
 cout << "Bit 1 positions: ";
 for ( unsigned int g = 0; g < xorArray.size(); g++)
 {
  cout << xorArray[g];
 }
 cout << "\n";
 cout << "Key length is: " << keyReg.size();
 cout << "\n";
 for ( unsigned int i = 0; i < keyReg.size(); i++)
 {
  if (keyReg[i]==1)
   {
    count1++;
   }
  else {
    count0++;
  }
 }
 cout << "Number of 0's: " << count0 << "\n";
 cout << "Number of 1's: " << count1 << "\n";
 if ( keyReg.size()%2 ==0)
  {
   cout << "key length is even. \n";
   if (count1==count0)
    {
   cout << "Key is perfect! \n";
    }
  else {
   cout << "Key is not perfect! \n";
    }
 }
  else
   {
  cout << "key length is odd. \n";
   if  ((count1==count0+1) || (count0==count1+1))
    {
   cout << "Key is perfect! \n";
    }
  else {
   cout << "Key is not perfect! \n";
    }
   }
  cin.get();
}

解决方案

One issue is that you didn't size the dynamic_bitsets before you used it. You should call dynamic_bitset::resize.

这篇关于使用C中的动态bitset的后段故障++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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