Ok, so when is a variable defined?
-
I took this from an online example. using VC++ 7 if you write...
int StudentAge; cout << "Student age = " << StudentAge << endl; cout << endl;
You will (rightly) get a runtime check failure since StudentAge has not been initialized. What I'd like to know is why THIS worksint StudentAge; int* ptrAge; ptrAge = &StudentAge; cout << "Student age = " << StudentAge << endl; cout << endl;
Here I don't get a runtime check failure even though it's clear StudentAge still hasn't been initialized. I'm just curious as to why this is the case. Thanks Woke up this morning...and got myself a blog -
I took this from an online example. using VC++ 7 if you write...
int StudentAge; cout << "Student age = " << StudentAge << endl; cout << endl;
You will (rightly) get a runtime check failure since StudentAge has not been initialized. What I'd like to know is why THIS worksint StudentAge; int* ptrAge; ptrAge = &StudentAge; cout << "Student age = " << StudentAge << endl; cout << endl;
Here I don't get a runtime check failure even though it's clear StudentAge still hasn't been initialized. I'm just curious as to why this is the case. Thanks Woke up this morning...and got myself a blogNeither case should issue a runtime error. The first case will issue a compiler warning (variable used without being initialized), depending on your compiler options. The second case is sufficient to mark the variable
StudentAge
as having been 'touched' (by the & operator), hence the warning wouldn't be appropriate. The compiler isn't (and shouldn't be, IMO) smart enough to look for secondary effects. Perhaps the variable gets initialized as a side effect, via the pointer.
Software Zen:
delete this;
-
Neither case should issue a runtime error. The first case will issue a compiler warning (variable used without being initialized), depending on your compiler options. The second case is sufficient to mark the variable
StudentAge
as having been 'touched' (by the & operator), hence the warning wouldn't be appropriate. The compiler isn't (and shouldn't be, IMO) smart enough to look for secondary effects. Perhaps the variable gets initialized as a side effect, via the pointer.
Software Zen:
delete this;
Thanks Gary, I used the term "Runtime Check Failure" because that's what the VC++7 warning dialog says (at runtime I might add). I guess I haven't set up my IDE to warn me on such things because the compile in the first case is clean as a whistle :-) Thanks!! Woke up this morning...and got myself a blog
-
Thanks Gary, I used the term "Runtime Check Failure" because that's what the VC++7 warning dialog says (at runtime I might add). I guess I haven't set up my IDE to warn me on such things because the compile in the first case is clean as a whistle :-) Thanks!! Woke up this morning...and got myself a blog
Hmmm. Is this Managed C++, by any chance? I'm just curious. I write 'unmanaged' C++, and have never seen this behavior at runtime.
Software Zen:
delete this;
-
Hmmm. Is this Managed C++, by any chance? I'm just curious. I write 'unmanaged' C++, and have never seen this behavior at runtime.
Software Zen:
delete this;
No it's plain C++ :-) Using VC++7. Woke up this morning...and got myself a blog
-
I took this from an online example. using VC++ 7 if you write...
int StudentAge; cout << "Student age = " << StudentAge << endl; cout << endl;
You will (rightly) get a runtime check failure since StudentAge has not been initialized. What I'd like to know is why THIS worksint StudentAge; int* ptrAge; ptrAge = &StudentAge; cout << "Student age = " << StudentAge << endl; cout << endl;
Here I don't get a runtime check failure even though it's clear StudentAge still hasn't been initialized. I'm just curious as to why this is the case. Thanks Woke up this morning...and got myself a blogFrom the compiler documentation, /RTC switch: [quote] /RTCu Reports when a variable is used without having been initialized. For example, an instruction that generates C4701 may also generate a run-time error under /RTCu. Any instruction that generates C4700 will generate a run-time error under /RTCu. However, consider the following code fragment: int a, *b, c; if ( 1 ) b = &a; c = a; // no run-time error with /RTCu If a variable could have been initialized, it will not be reported at run time by /RTCu. For example, after a variable is aliased through a pointer, the compiler will not track the variable and report uninitialized uses. In effect, you can initialize a variable by taking its address. The & operator works like an assignment operator in this situation. [/quote] [edit] Made attribution clearer. [/edit]
-
From the compiler documentation, /RTC switch: [quote] /RTCu Reports when a variable is used without having been initialized. For example, an instruction that generates C4701 may also generate a run-time error under /RTCu. Any instruction that generates C4700 will generate a run-time error under /RTCu. However, consider the following code fragment: int a, *b, c; if ( 1 ) b = &a; c = a; // no run-time error with /RTCu If a variable could have been initialized, it will not be reported at run time by /RTCu. For example, after a variable is aliased through a pointer, the compiler will not track the variable and report uninitialized uses. In effect, you can initialize a variable by taking its address. The & operator works like an assignment operator in this situation. [/quote] [edit] Made attribution clearer. [/edit]
Thanks Mike, looks like Gary was right :-) Woke up this morning...and got myself a blog