variable before initialization
A while ago, somebody said that VC++ initializes variables to 0 in debug, so it can be displayed. Does that mean that an uninitialized variable is different than an initialized variable (other than in value)?
If not, why not just display the crazy uninitialized value?
That might have been me (I heard it from someone else on this forum however). I''ve just tried it with Visual C++ 5:
New Project, Console:
Output in debug:
0
0.000000
Output in release:
0
0.000000
Ok, now the same with d and f local to main:
Output in debug:
6618628
0.000000
Output in release:
1
0.000000
That means a) I should check before I repeat what other people say or b) I did some emberassing [spellt right?] mistake.
I think the float will always be zero, not because it''s initialized, but because there is some option to correct invalid floats to zero (Well, I heard that a long time ago; Anyone knows for sure?).
New Project, Console:
#include <windows.h>#include <stdio.h>#include <conio.h>DWORD d;float f;int main(int, char * []){ printf("%d\n", d); printf("%f\n", f); _getch(); return 0;};
Output in debug:
0
0.000000
Output in release:
0
0.000000
Ok, now the same with d and f local to main:
Output in debug:
6618628
0.000000
Output in release:
1
0.000000
That means a) I should check before I repeat what other people say or b) I did some emberassing [spellt right?] mistake.
I think the float will always be zero, not because it''s initialized, but because there is some option to correct invalid floats to zero (Well, I heard that a long time ago; Anyone knows for sure?).
quote:
somebody said that VC++ initializes variables to 0 in debug,
Nope it doesn''t.
quote:
Yeah its so stupid! Only makes debugging harder...
Actually they do something different to aid in debugging. In a debug build variables on the stack are initialised to 0xcccccccc and variables on the heap are initialised to 0xcdcdcdcd. So if you see either of those values you know something is wrong (unless you actually do want that value)
The rules are different for global and static data though. Static variables are initialised to 0, and I can''t remember what the rules are for global.
By C standard:
All global variables are initialized to zeroes.
All static variables are uninitialized (Or initialized with 0xCDs in debug mode).
All global variables are initialized to zeroes.
All static variables are uninitialized (Or initialized with 0xCDs in debug mode).
Some info:
When you create a variable like this
int bob = 0; // You actually create a line of code.
This is true in both release and debug versions.
When you do this
int bob;
You create a variable. When that variable is first declared within a function is up to the compiler. In debug mode, at the very begining of the function, it sets the value to zero, not to just give it a default value, but to give it visibility to the rest of the function and more importantly, the debugger.
If this wasn't true, then you could not display that variable at all in the watch window, until it's first use.
On the other hand, in C++ release code, this particular variable will not have visibility until it's first use and beyond. It does not initialize it for many reasons. It gives the developer more control, the debugger does not need it(no debugger in release), it allows for faster code.
Remember C/C++ gives you more rope to hang yourself than many languages. But that rope, when used with proper technique, can give greater range.
For best results, (if possible) not initialize your variables to zero, but set them up with the real first value. I know, this is not always possible, but it's worth the attempt.
No: Int bob = 0;
later in code bob = 100;
Better:
Int bob = 100;
Edited by - Gorky on September 8, 2000 9:26:46 PM
When you create a variable like this
int bob = 0; // You actually create a line of code.
This is true in both release and debug versions.
When you do this
int bob;
You create a variable. When that variable is first declared within a function is up to the compiler. In debug mode, at the very begining of the function, it sets the value to zero, not to just give it a default value, but to give it visibility to the rest of the function and more importantly, the debugger.
If this wasn't true, then you could not display that variable at all in the watch window, until it's first use.
On the other hand, in C++ release code, this particular variable will not have visibility until it's first use and beyond. It does not initialize it for many reasons. It gives the developer more control, the debugger does not need it(no debugger in release), it allows for faster code.
Remember C/C++ gives you more rope to hang yourself than many languages. But that rope, when used with proper technique, can give greater range.
For best results, (if possible) not initialize your variables to zero, but set them up with the real first value. I know, this is not always possible, but it's worth the attempt.
No: Int bob = 0;
later in code bob = 100;
Better:
Int bob = 100;
Edited by - Gorky on September 8, 2000 9:26:46 PM
Hey, this is kind of interesting...
Anyone know any links on the workings of C compilers? I know the subject is fairly complex, with lots of theory, but I''m sure it would help somebody become a better programmer. Someday, I''ll buy a book on it, but until then, can anybody help me out?
Anyone know any links on the workings of C compilers? I know the subject is fairly complex, with lots of theory, but I''m sure it would help somebody become a better programmer. Someday, I''ll buy a book on it, but until then, can anybody help me out?
Generally there are 4 different kinds of data.
Initialized global data (including statics)
Uninitialized global data (and statics)
Stack data
Heap data
Initialized globals are stored in the obj file with their values, and are created with those values.
Uninitialized globals have no values stored in the obj file. The only data in the obj file for the entire unitialized segment is its total size. When beginning, this data section is allocated and set to 0s, so all unitialized globals and statics are set to 0.
Stack data is simply allocated off the stack as needed, and so is totally random (VC Debug version uses a default value, but that''s just informational). The program must initialize all stack variables before using them or the data will be random.
Heap data is also random (Again, VC debug may initialize it, but that''s just informational). The program must initialize it.
So in a nutshell, never assume a value unless you explicitely set it (even uninitialized globals, which should always be 0). If nothing else, its nice and readable.
Rock
Initialized global data (including statics)
Uninitialized global data (and statics)
Stack data
Heap data
Initialized globals are stored in the obj file with their values, and are created with those values.
Uninitialized globals have no values stored in the obj file. The only data in the obj file for the entire unitialized segment is its total size. When beginning, this data section is allocated and set to 0s, so all unitialized globals and statics are set to 0.
Stack data is simply allocated off the stack as needed, and so is totally random (VC Debug version uses a default value, but that''s just informational). The program must initialize all stack variables before using them or the data will be random.
Heap data is also random (Again, VC debug may initialize it, but that''s just informational). The program must initialize it.
So in a nutshell, never assume a value unless you explicitely set it (even uninitialized globals, which should always be 0). If nothing else, its nice and readable.
Rock
Always initialize variables... but don''t do this:
int i = 0;
If you didn''t use i in anywhere else, this is a stack waster, and compiler thinks that ''i'' is used and says nothing.
Do this instead:
int Big_Red_Diamond_Carbuncle;
Big_Red_Diamond_Carbuncle = where; // You tell me!
int i = 0;
If you didn''t use i in anywhere else, this is a stack waster, and compiler thinks that ''i'' is used and says nothing.
Do this instead:
int Big_Red_Diamond_Carbuncle;
Big_Red_Diamond_Carbuncle = where; // You tell me!
ANSI-ISO-SEC 9899-1999, Sec 6.7.8:
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly,
then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these
rules.
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly,
then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these
rules.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement