Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Ok, so when is a variable defined?

Ok, so when is a variable defined?

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Senkwe Chanda
    wrote on last edited by
    #1

    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 works int 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

    G M 2 Replies Last reply
    0
    • S Senkwe Chanda

      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 works int 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

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      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;

      S 1 Reply Last reply
      0
      • G Gary R Wheeler

        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;

        S Offline
        S Offline
        Senkwe Chanda
        wrote on last edited by
        #3

        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

        G 1 Reply Last reply
        0
        • S Senkwe Chanda

          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

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          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;

          S 1 Reply Last reply
          0
          • G Gary R Wheeler

            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;

            S Offline
            S Offline
            Senkwe Chanda
            wrote on last edited by
            #5

            No it's plain C++ :-) Using VC++7. Woke up this morning...and got myself a blog

            1 Reply Last reply
            0
            • S Senkwe Chanda

              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 works int 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

              M Offline
              M Offline
              Mike Dimmick
              wrote on last edited by
              #6

              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]

              S 1 Reply Last reply
              0
              • M Mike Dimmick

                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]

                S Offline
                S Offline
                Senkwe Chanda
                wrote on last edited by
                #7

                Thanks Mike, looks like Gary was right :-) Woke up this morning...and got myself a blog

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups