[ First ] [ Previous ] [ Next ] [ Last ] [ Manuals ]
Problems with Variables

This section covers problems related to variables.
A variable does not change
Problem
I have a variable and I assign it a value, but the value does not change in the debugger.
Background
You are not using the variable in the code. As a result, the compiler optimizes the final output file so that the variable is not used.
Solutions
Variables are assigned incorrect values
Problem
I notice that values seem to be changing incorrectly. I encounter one of these two problems:
Background
The compiler has recognized that the variables are not used concurrently, and has given the variables the same storage location. What you are seeing is a kind of automatic compiler optimization called "lifetime based register allocation," also known as "register coloring." Register coloring checks to see how variables are used in a routine. If two or more variables are in the same scope but are not used at the same time, the compiler may use the same processor register for both variables. Using registers instead of memory to store and manipulate variables improves a program's performance.
Listing 11.7 is a good example of the kind of code that results in register coloring. Because four different variables are set but never used simultaneously, the compiler has arranged for all four to use the same register. The debugger, however, has no way of knowing that all four variables share the same register, so it shows all four variables changing with each assignment.
Variables changing with register coloring:
void main(void)
{
long a = 0, b = 0, c = 0, d = 0;
a = 1; /* a is set to 1 */
a = 2; /* a is set to 2, b remains unchanged */
a = 3; /* a is set to 3, c remains unchanged */
a = 4; /* a is set to 4, d remains unchanged */
}
Solutions
Strange variable names
Problem
The debugger shows variables in the Variables pane or Global Variables window that are not declared in the source code.
Background
The compiler often creates its own temporary variables in the object code as it translates source code. These temporary variables have a dollar sign ($) in their names. The debugger also displays C++ virtual base class types with a $ prefix.
The compiler and linker often add variables from libraries and run-time routines that help initialize and terminate your program.
Solution
Strange data types
Problem
After I choose Data > Show Types, some enumerated values appears as having type "?anonx," where x is an arbitrary number.
Background
The debugger cannot display the names of enumerated types if the names are not defined in the source code. At compile time, the compiler assigns a generic type name to such enumerated types. It is this generic name that the debugger displays.
For example, in Listing 11.8, with Show Types selected, variable myMarx appears as having the anonymous type ?anonx, because its enumerated type has no name. On the other hand, variable myBeatle appears with type Beatle, because its enumerated type is defined with that name.
Unnamed enumerated types (C/C++):
// Debugger displays as anonymous type
enum {Groucho,
Harpo,
Chico,
Zeppo } myMarx = Harpo;
// Debugger displays as type Beatle
typedef enum Beatle {John,
Paul,
George,
Ringo} myBeatle = John;
Solution
Unrecognized data types
Problem
I declared my own data type. Why can I not view a variable as that type?
Background
The symbolics file includes information only about types that are used in the program. Types defined in typedef statements are not stored in the symbolics file, so you need to view the variable as the type from which it is derived. For example, if you declare a type MyLong based on the long data type, you can view it as a long, but not as a MyLong. For more information, refer to "Viewing data as different types".
Solution
"undefined identifier" in the Expressions window
Problem
A user-defined type in an expression in the Expressions window gives an "· undefined identifier ·" value.
Background
The debugger does not recognize data types that are simply aliases of another type, because such alias types are not included in the symbolics file.
For example, given the Pascal type declaration
  TYPE
   MYBIGINT = LONGINT;
the expression
  MYBIGINT(thePtr)
in the Expressions window will display its value as "· undefined identifier ·." To get the correct result, use the following expression instead:
  LONGINT(thePtr)
To learn more, see "Expression Limitations".
Solution
[ First ] [ Previous ] [ Next ] [ Last ] [ Manuals ]
Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.
Last updated: August 17, 2000