gpt4 book ai didi

c++ - 运行时检查失败 #3 变量 'i1' 被使用而没有被初始化

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:25 25 4
gpt4 key购买 nike

您好,我是该站点的新手,也是 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.
}

最佳答案

您正在使用 private( ... i1 ... ) OpenMP 数据子句,这意味着这些是本地未初始化的变量,在并行部分的范围之外是不可见的。您需要将初始化移到并行 block 中:

#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;

//...

关于c++ - 运行时检查失败 #3 变量 'i1' 被使用而没有被初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19726051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com