Unexplained C++ default int values

前端 未结 6 1607
悲哀的现实
悲哀的现实 2021-01-12 04:55

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

int arr[ARRAY_SIZE];

I set a break

6条回答
  •  Happy的楠姐
    2021-01-12 05:19

    -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.

    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++ 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.

提交回复
热议问题