Visual C++ - 运行时检查失败 #3 - 变量未初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9199644/
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
Visual C++ - Runtime Check Failure #3 - Variable is not initiliazed
提问by Ram Sidharth
I use Visual C++ 2010 Express Edition to compile and run the .exe files I write in the C++ programming language. I am trying to create a loop-based logic using C++ to ask the user how many entries he chooses to enter, and ask questions limited to that no. of entries. For example I want to output, "How many characters do you wish to enter?: " Say the user gives the answer as '3' which is stored in the int variable 'entries'. I then want to keep asking the question 3 times before it stops and continues with the next line of code. I hope you understand, here is a block of code to demonstrate what I am doing:
我使用 Visual C++ 2010 Express Edition 来编译和运行我用 C++ 编程语言编写的 .exe 文件。我正在尝试使用 C++ 创建一个基于循环的逻辑来询问用户他选择输入多少条目,并提出仅限于该编号的问题。条目。例如,我想输出,“您希望输入多少个字符?:”假设用户给出的答案为 '3',该答案存储在 int 变量 'entries' 中。然后我想在它停止并继续下一行代码之前继续问这个问题 3 次。我希望你明白,这里有一段代码来演示我在做什么:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "How many values do you need to enter?: ";
int entries;
cin >> entries;
int offset, number;
string valueName[50];
float valueValue[50];
for (offset = 0; offset < entries; offset++)
{
cout << "Enter " << number << " Value Name: ";
cin >> valueName[offset];
cout << "Enter " << valueName[offset] << "\'s value: ";
cin >> valueValue[offset];
for (number = 1; number <= entries; number++)
{
}
}
char response;
cin >> response;
return 0;
}
Strangely when I run this simple program, it fails when I enter the value's name to be inserted into the 0th element of the valueName[] array. It just pauses the execution of the program and a dialog box pops up saying "Runtime Check Failure #3 - Variable 'number' is being used without being initialized!" Another problem regarding this program is that, for quite some time, when I ran this program this "Runtime Check Failure #3" box never appeared, and when it didn't, the number value went wrong, and first started with 1, and then for the next loop jumped to 6, and then repeated 6 again for the next loop!
Please help me! I've checked online scouring this problem everywhere, but it just doesn't apply to my type of problem!
Is it because the variables are out of scope?
But they're declared outside the for loops right?
So please help me!
奇怪的是,当我运行这个简单的程序时,当我输入要插入到 valueName[] 数组的第 0 个元素的值的名称时,它会失败。它只是暂停程序的执行,并弹出一个对话框,上面写着“运行时检查失败 #3 - 变量‘编号’正在使用而没有被初始化!” 关于这个程序的另一个问题是,在很长一段时间里,当我运行这个程序时,这个“运行时检查失败#3”框从未出现过,当它没有出现时,数值出错了,首先从 1 开始,然后然后for下一个循环跳到6,然后下一个循环再次重复6!
请帮我!我已经在网上到处搜索这个问题,但它不适用于我的问题类型!
是因为变量超出范围吗?
但是它们是在 for 循环之外声明的,对吗?
所以请帮助我!
回答by eggbox
The runtime is telling you the truth, the following line comes after you have declared number as an int but have not given it a value.
运行时告诉您真相,以下行是在您将 number 声明为 int 但尚未为其赋予值之后出现的。
cout << "Enter " << number << " Value Name: ";
In your code you declare the following, in C++ this means give me 2 ints but the values are not defined yet, e.g.
在您的代码中,您声明以下内容,在 C++ 中,这意味着给我 2 个整数,但尚未定义值,例如
int offset, number;
Change it to something like this ..
把它改成这样..
int offset = 0;
int number = 0;
回答by Robert Kelly
The problem is exactly the error message you're getting. You're using the variable numberwithout initializing it.
问题正是您收到的错误消息。您正在使用变量number而不初始化它。
You use the variable right here, at the top of your loop, when it hasn't been initialized to anything yet:
当变量尚未初始化为任何内容时,您就在此处使用该变量,在循环的顶部:
cout << "Enter " << number << " Value Name: ";
What is your intention with the number variable? It doesn't really seem to be serving any purpose. If you want to print which entry you're currently on, you could use the offsetvariable instead, like this:
您对 number 变量的意图是什么?它似乎并没有真正起到任何作用。如果要打印当前所在的条目,可以改用offset变量,如下所示:
cout << "Enter " << offset << " Value Name: ";
But that still seems a little unclear to me.
但这对我来说仍然有点不清楚。
But the reason that you're having a problem is because the value is uninitialized, so you're experiencing undefined behavior. This is also the reason that Visual Studio doesn't always catch it; it will probably always catch in Debug mode, but in Release mode it will almost never catch it. You need to initialize all your variables before you use them.
但是您遇到问题的原因是该值未初始化,因此您遇到了未定义的行为。这也是 Visual Studio 并不总能捕捉到它的原因;它可能总是在调试模式下捕获,但在发布模式下它几乎永远不会捕获它。在使用它们之前,您需要初始化所有变量。
回答by Some programmer dude
You are printing the variable number
without assigning to it first, i.e. it's uninitialized. When it prints some random number it's because that what happens to be in the memory at the time you run the program. Assign a value to it before you use it.
您正在打印变量number
而不先分配给它,即它未初始化。当它打印一些随机数时,这是因为在您运行程序时内存中发生了什么。在使用之前为其分配一个值。
回答by Nav
In my case it was because an extern
variable was declared twice.
就我而言,这是因为一个extern
变量被声明了两次。