运行时检查失败#3变量'i1'正在使用而未初始化

时间:2013-11-01 11:41:06

标签: c++ c++11 openmp

您好我是该网站的新手,也是c ++ 11的新手,我已经尝试过调查此问题,我似乎收到了以下错误,“运行时检查失败#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.
}

2 个答案:

答案 0 :(得分: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;

//...

答案 1 :(得分:0)

您可以声明这些变量,例如i1,它们在打开并行区域之前初始化,但在其内部是私有的,为firstprivate