运行时检查失败 #3 变量 'i1' 正在使用而未初始化
Run Time Check Failure #3 the variable 'i1' is being used without being initialised
嗨,我是该网站和 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 数据子句,这意味着这些是局部未初始化的变量,在并行部分的范围之外不可见。您需要在并行块内移动初始化:
#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;
//...
您可以将那些变量(例如 i1
)声明为firstprivate
,这些变量在并行区域打开之前初始化,但在并行区域内部是私有的。
相关文章:
- 是变量初始化失败吗?
- thread_local静态成员模板定义:初始化失败,GCC
- 嵌套列表(字符串向量的向量)初始化失败
- 字符串初始化失败:"expression must have integral or unscoped enum type"
- C++中的即时对象初始化失败,出现LNK2005错误
- GLEW 初始化失败
- OpenCV 中的相机初始化失败
- C++全局对象初始化失败——为什么?以及是否可以将对象放入.DATA部分
- c++静态初始化失败示例
- 对象的c++数组,初始化失败
- C++11 指向同一基类的其他类的指针的非静态数据成员统一初始化失败
- C++ - 矢量初始化失败
- 初始化失败时如何终止qt编程
- 字符串初始化失败
- 变量的静态初始化失败
- 是块作用域静态或线程存储持续时间变量初始化失败的原因
- 继承构造函数+非默认构造函数类型的类内初始化失败
- Direct3d初始化失败/ c++
- 应用程序在Windows xp上初始化失败
- 系统构造函数初始化失败