简体   繁体   English

C ++:如何确保所有变量都已初始化?

[英]C++ : how to make sure all variables are initialized?

Recently I had lots of trouble with a non initialized variable. 最近,我遇到了一个非初始化变量的麻烦。

In Java, the default value of variable is null, therefore an exception is likely to be thrown when if the non-initialized variable is used. 在Java中,变量的默认值为null,因此,如果使用未初始化的变量,则可能会引发异常。 If I understood, in C++, the variable is initialized with whatever data turns out to be in the memory. 如果我理解的话,在C ++中,该变量将使用数据在内存中的任何形式进行初始化。 Which means that the program is likely to run, and it might be hard to even know there is something wrong with it. 这意味着该程序可能会运行,甚至可能很难知道它存在问题。

What would be the clean way to deal with this ? 解决这个问题的干净方法是什么? Is there some good programming habit that would reduce the risk ? 是否有一些良好的编程习惯可以降低风险? In my case, the variable was declared in the header file and should have been initialized in the cpp file, which is an example of things that makes error more likely. 就我而言,该变量是在头文件中声明的,并且应该已经在cpp文件中进行了初始化,这是使错误发生的可能性更大的示例。

thx 谢谢


Edition after receiving few answers: 版收到一些答案后:

My apologies, my question was not specific enough. 抱歉,我的问题不够具体。

The answer I get to use flag for the compilers to get informed of non-initialized variables will be useful. 我使用标记来使编译器了解未初始化变量的答案将很有用。

But there are rare cased variables can not be initialized at the beginning, because depending on the behavior of your system. 但是有少数情况下的变量无法在一开始就初始化,因为这取决于系统的行为。

in header file 在头文件中

double learnedValue;

in cpp file 在cpp文件中

/* code that has nothing to do with learnedValue
...
*/

learnedValue = a*b*c; // values of a, b and c computed in the code above

/*code making use of learned value
...
*/

Now what happened is that forgot the line "learnedValue=a*b*c". 现在发生的事情是忘记了“ learnedValue = a * b * c”这一行。

But the program was working good, just with value of learnedValue initialized with whatever what was in the memory when it was declared. 但是该程序运行良好,只是使用声明时使用内存中的内容初始化的learningValue的值。

In Java, such error is not an issue, because the code making use of learned value is likely to crash or throw an exception (at least you get to know what was wrong). 在Java中,这样的错误不是问题,因为利用学习后的值的代码很可能崩溃或引发异常(至少您会知道出了什么问题)。

In C++, you can apparently be happy and never get to know there is a problem at all. 在C ++中,您显然可以很快乐,甚至从不了解根本没有问题。 Or ? 要么 ?

Pls make sure you have appropriate warning levels set while compiling your program. 请确保在编译程序时设置了适当的警告级别。 Compilers issue appropriate warning whenever un-initialized variables are used. 每当使用未初始化的变量时,编译器都会发出适当的警告。

On g++, -Wall compiler option would show all warnings. 在g ++上,-Wall编译器选项将显示所有警告。

On Visual studio, you might have to use warning level 4. 在Visual Studio上,您可能必须使用警告级别4。

Also, there are some static code analysis tool available in the market. 另外,市场上有一些静态代码分析工具。 cppCheck is one such tool available for free. cppCheck是这样一种免费提供的工具。

You should not define a variable in a header (only declare it). 您不应在标头中定义变量(只能声明它)。 Otherwise you will get other errors when you include the header in several .cpp files. 否则,当您在多个.cpp文件中包含标头时,您将得到其他错误。

When actually defining a variable, you can also give it an initial value (like 0). 在实际定义变量时,还可以给它一个初始值(例如0)。 In C++ it is also common to defer the definition of (local) variables until you have a value to assign to them. 在C ++中,通常将(局部)变量的定义推迟到您有一个要分配给它们的值之前。

In the header file 在头文件中

extern double learnedValue;
^^^^^^

In the cpp file 在cpp文件中

double learnedValue = 0;

/* code that has nothing to do with learnedValue
...
*/

learnedValue = a*b*c; // values of a, b and c computed in the code above

/*code making use of learned value
...
*/

您可以在声明变量的地方定义变量

c++11 allows you to initialize variables inside class. c ++ 11允许您在类内部初始化变量。 If that is not implemented by the compiler yet then the constructor initialization list is the area to check. 如果编译器尚未实现,则构造器初始化列表是要检查的区域。

The C# can initialize the variable. C#可以初始化变量。 But C++ not, so when use a pointer without initialized, it always throw exception. 但是C ++不能,所以当使用没有初始化的指针时,它总是抛出异常。 You should make a good habit to initialize all the variables in the class constructor. 您应该养成在类构造函数中初始化所有变量的良好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在构造函数中初始化的变量未保持初始化状态(C ++) - Variables initialized in constructor not staying initialized (c++) 如何确保多核服务器上的所有内核都运行多线程 C++ 程序? - How to make sure that a multithreaded C++ program is run by ALL cores on a multicore server? 如何在程序退出之前确保所有c ++ boost线程完成? - How can I make sure all of my c++ boost threads finish before the program exits? C ++中未初始化变量的默认值是什么? - What is the default value for not initialized variables in C++? C ++匿名命名空间:变量初始化为0? - C++ anonymous namespace: Variables initialized to 0? 如何确保在c ++中解锁储物柜? 哪种解决方案更好 - how to make sure locker be unlock in c++? which solution is better C ++如何确保通过共享指针回收内存 - c++ how to make sure memory is reclaimed via shared pointers 如何确保数据类型与C ++中的数据类型一样大 - How to make sure a data type is as large as it needs to be in C++ 如何确保只有 1 个线程在运行? C++ - How to make sure only 1 thread is running? C++ GPU(Metal)上的C++类型,如何将另一个变量的类型保存到一个类中,并确保所有类实例的大小相同? - C++ type on GPU(Metal), how to save the type of another variable into a class, and make sure all the class instance have the same size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM