9.17 Initialize global variables

9.17 Initialize global variables

9.17.1 Basic tactic

Use compiler options to initialize statically with unusual or invalid values those global variables that aren’t explicitly initialized.

9.17.2 Purpose

Identify uninitialized variables that need to be initialized.

9.17.3 Questions answered

  • Which global variables are getting random values that are being used in computation?

  • Where is the first place that the random values are being used?

9.17.4 Potential problems

Computations may not fail with the values assigned.

9.17.5 Refined tactics

Initialize floating-point variables to IEEE Not a Number (NaN), pointer variables to an invalid address, and integer variables to the maximum representable integer.

9.17.6 Related tactics

  • Add code that initializes all global variables.

  • Run on a different system, where 0 isn’t a valid address.

9.17.7 Choosing tactics

Use the refined tactic when your compiler doesn’t support an option for the basic tactic.

Use basic or related tactics when any of the following is true:

  • A bug occurs when the program is run standalone, but not when it’s run under a debugger.

  • A bug occurs when the program is run under one user account (and environment), but not when it’s run under another.

Use this tactic at least once before the program is put into production, as an additional testing strategy.

9.17.8 Language specifics

  • C++: Use constructors to ensure that all class members are properly initialized.

  • Java: Java doesn’t have global variables. Class members are always initialized, so the tactic is unnecessary.

  • C: Some C implementations initialize all global variables to 0, while others do not. If you aren’t using a compiler that conforms to the C99 standard, don’t rely on this behavior.

  • Fortran: Some Fortran implementations initialize all numeric variables in COMMON to 0. You shouldn’t rely on this behavior if you aren’t using a compiler that guarantees this behavior.

Debugging by Thinking. A Multidisciplinary Approach
Debugging by Thinking: A Multidisciplinary Approach (HP Technologies)
ISBN: 1555583075
EAN: 2147483647
Year: 2002
Pages: 172

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net