2013-11-01 194 views
1

嗨我是新來的網站,也是c + + 11,我試圖調查這一點,我似乎得到以下錯誤,「運行時間檢查失敗#3變量'i1'正在被使用而未被初始化」我認爲這正是錯誤所說的,我並沒有初始化變量,但我已經檢查了一遍又一遍,似乎無法察覺爲什麼它沒有被初始化。希望有人可以提供幫助。 這裏是我的代碼:運行時間檢查失敗#3變量'i1'正在使用未經初始化

void cipher(array<char, array_rows>& text1, array<char, array_rows>& text2, array<int, 52>& key) 
{ 

int i1=0;     // Index into first text array. 
int i2=0;     // Index into second text array. 
int ik=0;      // Index into key array. 
int x1=0; 
int x2=0; 
int x3=0; 
int x4=0; 
int t1=0; 
int t2=0; // Four "16-bit" blocks, two temps. 
int r=0;      // Eight rounds of processing. 
int i=0; 

auto num_threads = thread::hardware_concurrency(); 


#pragma omp parallel num_threads(num_threads) default(none) shared(text1, text2, key) private(i, i1,i2,ik,x1,x2,x3,x4,t1,t2,r) 
//#pragma for <----- this commented or not gives same error 
for (i = 0; i < text1.size(); i += 8) 
{ 
    ik = 0;     // Restart key index. 
    r = 8;     // Eight rounds of processing. 

    // Load eight plain1 bytes as four 16-bit "unsigned" integers. 
    // Masking with 0xff prevents sign extension with cast to int. 

    x1 = text1[i1++] & 0xff;   // Build 16-bit x1 from 2 bytes, 
    x1 |= (text1[i1++] & 0xff) << 8; // assuming low-order byte first. 
    x2 = text1[i1++] & 0xff; 
    x2 |= (text1[i1++] & 0xff) << 8; 
    x3 = text1[i1++] & 0xff; 
    x3 |= (text1[i1++] & 0xff) << 8; 
    x4 = text1[i1++] & 0xff; 
    x4 |= (text1[i1++] & 0xff) << 8; 

    do 
    { 
     // 1) Multiply (modulo 0x10001), 1st text sub-block 
     // with 1st key sub-block. 

     x1 = (int) ((long long) x1 * key[ik++] % 0x10001L & 0xffff); 

     // 2) Add (modulo 0x10000), 2nd text sub-block 
     // with 2nd key sub-block. 

     x2 = x2 + key[ik++] & 0xffff; 

     // 3) Add (modulo 0x10000), 3rd text sub-block 
     // with 3rd key sub-block. 

     x3 = x3 + key[ik++] & 0xffff; 

     // 4) Multiply (modulo 0x10001), 4th text sub-block 
     // with 4th key sub-block. 

     x4 = (int) ((long long) x4 * key[ik++] % 0x10001L & 0xffff); 

     // 5) XOR results from steps 1 and 3. 

     t2 = x1^x3; 

     // 6) XOR results from steps 2 and 4. 
     // Included in step 8. 

     // 7) Multiply (modulo 0x10001), result of step 5 
     // with 5th key sub-block. 

     t2 = (int) ((long long) t2 * key[ik++] % 0x10001L & 0xffff); 

     // 8) Add (modulo 0x10000), results of steps 6 and 7. 

     t1 = t2 + (x2^x4) & 0xffff; 

     // 9) Multiply (modulo 0x10001), result of step 8 
     // with 6th key sub-block. 

     t1 = (int) ((long long) t1 * key[ik++] % 0x10001L & 0xffff); 

     // 10) Add (modulo 0x10000), results of steps 7 and 9. 

     t2 = t1 + t2 & 0xffff; 

     // 11) XOR results from steps 1 and 9. 

     x1 ^= t1; 

     // 14) XOR results from steps 4 and 10. (Out of order). 

     x4 ^= t2; 

     // 13) XOR results from steps 2 and 10. (Out of order). 

     t2 ^= x2; 

     // 12) XOR results from steps 3 and 9. (Out of order). 

     x2 = x3^t1; 

     x3 = t2;  // Results of x2 and x3 now swapped. 

    } while(--r != 0); // Repeats seven more rounds. 

    // Final output transform (4 steps). 

    // 1) Multiply (modulo 0x10001), 1st text-block 
    // with 1st key sub-block. 

    x1 = (int) ((long long) x1 * key[ik++] % 0x10001L & 0xffff); 

    // 2) Add (modulo 0x10000), 2nd text sub-block 
    // with 2nd key sub-block. It says x3, but that is to undo swap 
    // of subblocks 2 and 3 in 8th processing round. 

    x3 = x3 + key[ik++] & 0xffff; 

    // 3) Add (modulo 0x10000), 3rd text sub-block 
    // with 3rd key sub-block. It says x2, but that is to undo swap 
    // of subblocks 2 and 3 in 8th processing round. 

    x2 = x2 + key[ik++] & 0xffff; 

    // 4) Multiply (modulo 0x10001), 4th text-block 
    // with 4th key sub-block. 

    x4 = (int) ((long long) x4 * key[ik++] % 0x10001L & 0xffff); 

    // Repackage from 16-bit sub-blocks to 8-bit byte array text2. 

    text2[i2++] = (char)x1; 
    text2[i2++] = (char)(x1 >> 8); 
    text2[i2++] = (char)x3;    // x3 and x2 are switched 
    text2[i2++] = (char)(x3 >> 8);  // only in name. 
    text2[i2++] = (char)x2; 
    text2[i2++] = (char)(x2 >> 8); 
    text2[i2++] = (char)x4; 
    text2[i2++] = (char)(x4 >> 8); 

} // End for loop. 
} 
+0

但你有'int i1 = 0; ' - 你確定這是導致錯誤的代碼嗎? – doctorlove

+0

@doctorlove如果這將是「正常」的C++,那麼你會是正確的,但是OpenMP的使用在這裏是問題。 –

回答

1

您使用的是private(... i1 ...) OpenMP的數據條款,這意味着,這些都是局部未初始化的變量不屬於並行區域範圍的外部可見。您需要將初始化平行塊:

#pragma omp parallel num_threads(num_threads) default(none) shared(text1, text2, key) private(i, i1,i2,ik,x1,x2,x3,x4,t1,t2,r) 

i=i1=i2=ik=x1=x2=x3=x4=t1=t2=r=0; 

//... 
+0

非常感謝它的魅力 –

0

你可以聲明這些變量,如i1,這是之前的並行區域的開放,但裏面的私人初始化,是firstprivate

相關問題