Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

boost - Segmentation fault after using dynamic bitset in C++

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: 
";
 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 << "
";
 operSeq = inpSeq;
 keyReg.push_back(inpSeq[0]);
  int x = xorArray[0];
  cout << "x is: " << x << "
";
  for ( unsigned int  i = 0; i < xorArray.size();  i++)
  {
   cout << xorArray[i] << "
";
  }
  cout << "bit 3 of initial " << bit[seq_end] << "
";
  do {
  for (unsigned int r = 1; r < xorArray.size(); r++)
  {
  bit[seq_end] = operSeq[x];
  cout << "bit 3 from prev: " << bit[seq_end] << "
";
  y = xorArray[r];
//  cout << "opseq[y] is: " << operSeq[y] << "
";
  bit[seq_end] = bit[seq_end] ^ operSeq[y];
//  cout << "bit[3] after xor: " << bit[seq_end] << "
";
  }
  operSeq >>= 1;
//  cout <<"operSeq after shift: " <<  operSeq << "
";
  operSeq[seq_end]  = bit[seq_end];
//  cout <<"opserSeq bit 4 after = bit[3]: " << operSeq[seq_end] << "
";
//  cout <<"new operSeq: " << operSeq << "
";
  keyReg.push_back(operSeq[0]);
  turnCount ++;
  cout << "--
";
 }
 while ((operSeq != inpSeq) && (turnCount < 20));
 cout << "Generated key is: ";
 for (unsigned int k = 0; k < keyReg.size(); k++)
  {
  cout  <<  keyReg[k];
  }
 cout << "
";
 cout << "Bit 1 positions: ";
 for ( unsigned int g = 0; g < xorArray.size(); g++)
 {
  cout << xorArray[g];
 }
 cout << "
";
 cout << "Key length is: " << keyReg.size();
 cout << "
";
 for ( unsigned int i = 0; i < keyReg.size(); i++)
 {
  if (keyReg[i]==1)
   {
    count1++;
   }
  else {
    count0++;
  }
 }
 cout << "Number of 0's: " << count0 << "
";
 cout << "Number of 1's: " << count1 << "
";
 if ( keyReg.size()%2 ==0)
  {
   cout << "key length is even. 
";
   if (count1==count0)
    {
   cout << "Key is perfect! 
";
    }
  else {
   cout << "Key is not perfect! 
";
    }
 }
  else
   {
  cout << "key length is odd. 
";
   if  ((count1==count0+1) || (count0==count1+1))
    {
   cout << "Key is perfect! 
";
    }
  else {
   cout << "Key is not perfect! 
";
    }
   }
  cin.get();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...