警告:未初始化的变量 //但是我已经初始化了!C++ 编译器错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6813660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning: uninitialized variable //But I have initialized ! C++ Compiler bug?
提问by Davit Tvildiani
Iam trying to compile this program but i get warning and when i run vc++ 2010 debugger pops up : ( Here is my code :
我试图编译这个程序,但我收到警告,当我运行 vc++ 2010 调试器时弹出:( 这是我的代码:
#include <iostream>
using namespace std;
int num;
int min(int mas[])
{
int i,minn,index; /* But I have declared them : (((( */
for(i=0;i<num;i++)
{
if(mas[i]!=0)minn=mas[i];
break;
}
if(i==num) return 0;
for(i=0;i<num;i++)
if(mas[i]!=0 && minn>mas[i])
{
minn=mas[i];
index=i;
}
mas[index]=0;
return minn;
}
int main()
{
cin>>num;
int *array=new int[num]; int tmp;
tmp=min(array);
}
and Here is a compiler log :
这是一个编译器日志:
prog.cpp: In function ‘int min(int*)':
prog.cpp:6: warning: ‘index' may be used uninitialized in this function
prog.cpp:6: warning: ‘minn' may be used uninitialized in this function
What i am doing wrong ? or its is compiler bug ? :) Thank you :)
我做错了什么?或者它是编译器错误?:) 谢谢 :)
回答by ptomato
You have declaredthem, but not initializedthem. Simply write int minn = 0, index = 0;
to avoid the warning. If you don't initialize a variable, its default value is whatever was at that location in memory already; usually garbage.
你已经声明了它们,但没有初始化它们。只需写入int minn = 0, index = 0;
以避免警告。如果你不初始化一个变量,它的默认值是内存中那个位置已经存在的值;通常是垃圾。
The thing is, if num
is negative, then neither of the for loops in your min()
function will execute, and so minn
and index
will not have been assigned values. The if(i == num)
test also won't break out of the function and prevent this from happening. So the last two lines of the function will have completely undefined results.
问题是,如果num
为负,则既不的在您的循环min()
功能将执行,所以minn
并index
不会被赋值。该if(i == num)
测试还不会爆发的功能,并防止这种情况发生。所以函数的最后两行将有完全未定义的结果。
Sometimes there really isn't a path for the variables to be used uninitialized, though; sometimes the compiler just isn't quite smart enough to figure out all the subtleties. Just give them an initial value to avoid the warning.
但是,有时确实没有用于未初始化变量的路径;有时编译器不够聪明,无法弄清楚所有的微妙之处。只需给它们一个初始值即可避免警告。
回答by Kugel
Declaration != initialization. When you declare them the variables have random values. Just initialize them to sensible values like -1 for index and minn to a INT_MAX.
声明 != 初始化。当您声明它们时,变量具有随机值。只需将它们初始化为合理的值,例如 -1 表示 index 并将 minn 初始化为 INT_MAX。
回答by Djole
But you haven't initialized them : ))))
EX: int i,minn=0,index=0;
Imagine that you pass num
that equals 0
, at the end you would be returning uninitialized value of minn
and just before that you would set mas[unknown_number]=0;
which will probably cause your app to crash since you will be referencing memory that is most likely beyond your scope. You should do a check in the beggining like if(num<1)return -1;
但是你还没有初始化它们 :)))) EX:int i,minn=0,index=0;
想象一下,你传递num
了 equals 0
,最后你将返回未初始化的值,minn
就在此之前你会设置mas[unknown_number]=0;
这可能会导致你的应用程序崩溃,因为你会引用最有可能超出您范围的内存。你应该在开始时检查一下if(num<1)return -1;
回答by dascandy
Suppose the entire array you pass in is 0. Both loops short-circuit and never execute, both minn and index are uninitialized.
假设您传入的整个数组为 0。两个循环都短路并且永远不会执行,minn 和 index 都未初始化。
Now if this happens, what should be happening? Set the variables to the values that accomplish just that.
现在如果发生这种情况,应该发生什么?将变量设置为实现这一目标的值。
回答by Gary
As you say in your comment, yes, you have declaredyour variables, but you haven't initialized them. Initializing a variable means giving it a value. So in this case, you have told the compiler that you want to create three integers, but you haven't told it what values you want to store in those integers. That would be ok if, for every possible path through your function, index and minn were guaranteed to be given a value, but the problem here is that there is a path through your function where minn and index will never be initialized. First of all, here:
正如您在评论中所说,是的,您已经声明了变量,但尚未初始化它们。初始化一个变量意味着给它一个值。所以在这种情况下,您已经告诉编译器您想要创建三个整数,但您没有告诉它您想在这些整数中存储哪些值。如果对于通过您的函数的每条可能路径, index 和 minn 都保证被赋予一个值,那将是可以的,但这里的问题是您的函数中有一条路径,其中 minn 和 index 永远不会被初始化。首先,这里:
for(i=0;i<num;i++)
{
if(mas[i]!=0)minn=mas[i];
break;
}
If you have an array of zeros, then minn is never initialized to a value.
如果您有一个零数组,则 minn 永远不会初始化为值。
Then further down:
然后再往下:
for(i=0;i<num;i++)
if(mas[i]!=0 && minn>mas[i])
{
minn=mas[i];
index=i;
}
first of all, if you had an array of zeros, well what is the value in minn? There is no value. You are asking the compiler to compare mas[i] to a number which doesn't exist. Furthermore, what if mas[i] is always equal to zero? Well now you don't initialize minn or index. Yet at the end of the function, you are attempting to use the value of index to get an integer from the array amd then you return minn (which still equals nothing).
首先,如果你有一个零数组,那么 minn 的值是多少?没有价值。您要求编译器将 mas[i] 与不存在的数字进行比较。此外,如果 mas[i] 总是等于零怎么办?那么现在你不初始化 minn 或 index. 然而,在函数的末尾,您试图使用 index 的值从数组 amd 中获取一个整数,然后返回 minn (它仍然不等于任何内容)。
That's the problem you're getting from the compiler. It can see this potential outcome and is warning you that your function can be broken due to these integers never getting a value. To fix it, do what the other lads have suggested and let index and minn equal zero at the start.
这就是你从编译器那里得到的问题。它可以看到这种潜在的结果,并警告您,由于这些整数从未获得值,您的函数可能会被破坏。要解决它,请按照其他小伙子的建议进行操作,并在开始时让 index 和 minn 等于 0。