在C++中使用动态位集后出现分段错误
Segmentation fault after using dynamic bitset in C++
我正在尝试制作一个可变长度的线性反馈移位寄存器,所以我使用了boost库中的动态位集而不是位集。编译程序后,在运行程序时,它在显示xorArray的内容后直接产生Segmentation错误。我认为错误在于动态位集变量的定义,但我无法理解。有三个变量,inpSeq、operSeq和bit。这是代码:
#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();
}
一个问题是在使用dynamic_bitsets之前没有对其进行大小调整。您应该调用dynamic_bitset::resize
。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?