2014-04-09 44 views
-1

我試圖製作一個可變長度的線性反饋移位寄存器,所以我使用動態位集代替了位集的增強庫。在編譯程序並運行它之後,它在顯示xorArray的內容之後直接產生了分段錯誤。我認爲錯誤在於動態位變量的定義,但我無法弄清楚。有三個變量,inpSeq,operSeq和bit。以下是代碼:在C++中使用動態位集之後的分段錯誤

#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(); 
} 
+0

我不知道什麼是「可變長度線性反饋移位寄存器」。也許你可以詳細說明。 –

+0

線性反饋移位寄存器獲取一系列比特,稱爲初始卡,例如1010,它對由多項式指定的比特進行異或運算,例如x^4 + x^3 + x^0,在這種情況下,它將xor的第一個和最後一個比特,但在將序列向右移位一位後,最後一個比特的結果。我的代碼在這裏忽略了x^4,當我使用bitset時,代碼工作正常,序列僅固定爲4位長度。但我希望它是可變的。 –

+1

「進入多項式後直接出現故障」 - 顯然不是,因爲接下來的幾十行都沒問題。在最後刪除廢話的行,直到你有足夠的錯誤,然後我們可以集中我們的努力來幫助你。 -1,因爲努力和考慮不周。 –

回答

3

一個問題是,在使用它之前,您沒有調整dynamic_bitsets的大小。您應該致電dynamic_bitset::resize

+0

我發現動態位集的文檔有點複雜,事實上,我是整個C++編程和編程的初學者。請您提供一個我需要對我的源代碼進行編輯的例子嗎? –

+1

看看「構造函數」部分。您正在使用默認構造函數構造一個dynamic_bitset。如果你閱讀文檔,默認構造一個dynamic_bitset會創建一個大小爲0. – PaulMcKenzie

+0

「bool value = false」的目的是什麼? –