Magic Numbers in Visual C++

Uwaga! Informacje na tej stronie mają ponad 5 lat. Nadal je udostępniam, ale prawdopodobnie nie odzwierciedlają one mojej aktualnej wiedzy ani przekonań.

# Magic Numbers in Visual C++

Aug 2009

Uninitialized variables in C++ language are not filled automatically. They can contain any random data. It's as good as no range checking in access to arrays, because it doesn't waste time out of programmer's control. But on the other hand this rule makes us programmers responsible for thinking about when, where and how do we ensure that we don't use variables before their initialization or index arrays out of their range.

Visual C++ helps us with that. While in Release compilation, variables and fields are really uninitialized and contain random data, but under Debug configuration they are filled with special magic numbers. If we debug the program, hexadecimal codes of these numbers can tell us about the kind of error we made in the code. Just press "Hex" button on Debug toolbar to see all numbers as hexadecimal. Here are some of magic numbers used by Visual C++:

Interesting thing is that compiler doesn't warn about uninitialized fields in classes, despite it knows about uninitialized local variables and shows "Run-Time Check Failure #3 - The variable 'v' is being used without being initialized.". So whenever you have a bug which appears only in Release configuration and not in Debug, good chances are that you've forgotten to initialize a field in a class.

By the way, some programmers think that debugging is possible only in Debug configuration (as its name says). You acually can debug also in Release mode, although step by step execution may sometimes work strange due to optimizations like inlining, some watches may show invalid values and sometimes looking into objects doesn't work (probably bacause of optimized way "this" pointer is passed).

Comments | #visual studio #c++ Share


[Stat] [STAT NO AD] [Download] [Dropbox] [pub] [Mirror]
Copyright © 2004-2017