本文介绍了原因不明的C ++默认值INT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经重构了一些code和我注意到涉及一个未初始化的int数组一些靠不住的行为:

I've been refactoring some code and I noticed some wonky behavior involving an uninitialized int array:

int arr[ARRAY_SIZE];

我设置一个断点,似乎所有值默认为-858993460。是不是有什么特别的这个价值?任何想法,为什么他们不默认为0?

I set a break-point and it seems all values default to -858993460. Is there something special with this value? Any ideas why they don't default to 0?

推荐答案

-858993460,在十六进制,CCCCCCCC,它的Visual Studio把默认的调试模式。这是为了让你更容易注意到你忘了初始化变量。在发行模式下,它可以是任何东西。

-858993460 is, in hex, CCCCCCCC, which Visual Studio puts as default in DEBUG MODE. This is to make it easier for you to notice that you forgot to initialize the variable. In release mode it could be anything.

实际上,我不清楚为什么 mouseBufferX 不是10个项目的元素(如果这样做了编译,这是不是10元)。但我pretty确保标准说静nonstatics之前被初始化。不管怎么说,我个人不喜欢使用定义常量 s到申报整数。使用一个枚举来代替。

I’m actually unsure why mouseBufferX isn’t an element of 10 items (if this does compile and that isn’t 10 elements). But I am pretty sure that the standard says statics are initialized before nonstatics. Anyways, I personally hate using defines and consts to declare ints. Use an enum instead.

C ++不预设任何0,所以你必须在使用前分配的东西值。静态变量是此规则的例外,因为他们是默认设置为零。但我会注意,使用静态变量是不鼓励,有的语言(如C#)不允许你在一个函数声明静态变量。

C++ doesn’t default anything to 0, so you MUST assign something a value before using it. Static variables are exceptions to this rule as they are set to zero by default. But I’ll note that the use of static variables is discouraged, and some languages (such as C#) do not allow you to declare static variables in a function.

这篇关于原因不明的C ++默认值INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 11:47