运行-时间错误,使用未声明的变量,C++
Run-time error, using undeclared variable, C++
我在 运行 期间遇到了这个非常奇怪的错误。
我的程序有两个参数,对它们进行一些数学运算,最后得到 std::cout
的值。
如果我输入一些值,我的程序可以运行,但是如果我输入其他值,它会说正在使用一个变量而没有被初始化,我认为这是没有意义的。
代码如下:
#include <iostream>
#include <stdio.h>
#include <cmath>
double align_nb(int n) { return { ceil(n / 512.0)*512.0 }; } // bytes
double align_pt(int k) { return { floor(k / 512.0)*512.0 }; } // pointer
int main(int argc, char * argv[])
{
int o_n = std::atoi(argv[1]); // original number
int o_p = std::atoi(argv[2]); // original pointer
int max_bytes, new_pointer; // max bytes to read, new pointer to point
float log = (std::log(o_n) / std::log(2));
if (log != floor(log))
{
max_bytes = align_nb(o_n); // bytes alinhados para a frente
new_pointer = align_pt(o_p); // ponteiro alinhado atrás
}
else if (log == floor(log))
{
new_pointer = align_pt(o_p);
if (max_bytes + (o_p - new_pointer) >max_bytes)
{
max_bytes += 512;
}
}
std::cout << "Original bytes= " << o_n << std::endl;
std::cout << "Original pointer= " << o_p << std::endl;
std::cout << "Max_bytes= " << max_bytes << std::endl;
std::cout << "new_pointer= " << new_pointer << std::endl;
return 0;
}
这是我测试它的值,它崩溃了,给了我 运行-time 错误:
2048 513
1024 500
这是一个值示例,代码没有给我该错误并且程序运行正常:
513 520
Here 是它给我的错误的打印。
我非常感谢有人向我解释为什么 error/how 可以修复它。
不管怎样,谢谢!
(PS: 包含数学标签,因为它可能是程序中的数学导致它崩溃。如果有人认为它不应该用在这个问题中,请在评论中告诉我,我删除它。)
(PS 2: 当它给我 运行 时间错误时它抱怨的变量是 'max_bytes'。)
如果您的代码在第 17 行采用 else 路径,那么您的代码不会初始化 max_bytes
,但会在之后使用它。就是这个问题。
备注:
- 比较计算出的浮点值是否相等通常是一种不好的做法
- 您不需要第 23 行的附加 if。
确保对于每个路径,您的代码都采用您使用的变量的值进行初始化。如果你不这样做,你就会得到所谓的未定义行为。未初始化的变量中可能有任何内容。
int max_bytes;
....
....
expression_involving_max_byte <- Dangerous!
我在 运行 期间遇到了这个非常奇怪的错误。
我的程序有两个参数,对它们进行一些数学运算,最后得到 std::cout
的值。
如果我输入一些值,我的程序可以运行,但是如果我输入其他值,它会说正在使用一个变量而没有被初始化,我认为这是没有意义的。
代码如下:
#include <iostream>
#include <stdio.h>
#include <cmath>
double align_nb(int n) { return { ceil(n / 512.0)*512.0 }; } // bytes
double align_pt(int k) { return { floor(k / 512.0)*512.0 }; } // pointer
int main(int argc, char * argv[])
{
int o_n = std::atoi(argv[1]); // original number
int o_p = std::atoi(argv[2]); // original pointer
int max_bytes, new_pointer; // max bytes to read, new pointer to point
float log = (std::log(o_n) / std::log(2));
if (log != floor(log))
{
max_bytes = align_nb(o_n); // bytes alinhados para a frente
new_pointer = align_pt(o_p); // ponteiro alinhado atrás
}
else if (log == floor(log))
{
new_pointer = align_pt(o_p);
if (max_bytes + (o_p - new_pointer) >max_bytes)
{
max_bytes += 512;
}
}
std::cout << "Original bytes= " << o_n << std::endl;
std::cout << "Original pointer= " << o_p << std::endl;
std::cout << "Max_bytes= " << max_bytes << std::endl;
std::cout << "new_pointer= " << new_pointer << std::endl;
return 0;
}
这是我测试它的值,它崩溃了,给了我 运行-time 错误:
2048 513
1024 500
这是一个值示例,代码没有给我该错误并且程序运行正常:
513 520
Here 是它给我的错误的打印。
我非常感谢有人向我解释为什么 error/how 可以修复它。 不管怎样,谢谢!
(PS: 包含数学标签,因为它可能是程序中的数学导致它崩溃。如果有人认为它不应该用在这个问题中,请在评论中告诉我,我删除它。)
(PS 2: 当它给我 运行 时间错误时它抱怨的变量是 'max_bytes'。)
如果您的代码在第 17 行采用 else 路径,那么您的代码不会初始化 max_bytes
,但会在之后使用它。就是这个问题。
备注:
- 比较计算出的浮点值是否相等通常是一种不好的做法
- 您不需要第 23 行的附加 if。
确保对于每个路径,您的代码都采用您使用的变量的值进行初始化。如果你不这样做,你就会得到所谓的未定义行为。未初始化的变量中可能有任何内容。
int max_bytes;
....
....
expression_involving_max_byte <- Dangerous!