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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Global vs local variables

Global vs local variables

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studioquestion
8 Posts 5 Posters 1 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.
  • R Offline
    R Offline
    rajeevktripathi
    wrote on last edited by
    #1

    Hi All I have a confusion related to global and local variables... why global variables are initialized to zero whereas local variables are not... What is the reason behind this. Please suggest me the answer. Thanks & Regards

    W N D 3 Replies Last reply
    0
    • R rajeevktripathi

      Hi All I have a confusion related to global and local variables... why global variables are initialized to zero whereas local variables are not... What is the reason behind this. Please suggest me the answer. Thanks & Regards

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #2

      Global variables can be used within any function from anywhere within your code, local variables can only be used within scope that they were created ( usualy a single function ). It's good practice and reduces many bugs if you initialize all your variables when you create them, regardless of what the books tell you.

      R 1 Reply Last reply
      0
      • W Waldermort

        Global variables can be used within any function from anywhere within your code, local variables can only be used within scope that they were created ( usualy a single function ). It's good practice and reduces many bugs if you initialize all your variables when you create them, regardless of what the books tell you.

        R Offline
        R Offline
        rajeevktripathi
        wrote on last edited by
        #3

        thanks WalderMort it is fine that we should initialize each var while creating it, but wondering that why global variables are initialized to zero by compiler whereas local variables are not.. Please help to understand the logic Thanks & Regards

        W 1 Reply Last reply
        0
        • R rajeevktripathi

          thanks WalderMort it is fine that we should initialize each var while creating it, but wondering that why global variables are initialized to zero by compiler whereas local variables are not.. Please help to understand the logic Thanks & Regards

          W Offline
          W Offline
          Waldermort
          wrote on last edited by
          #4

          In a debug build, variables are initialized, but you will find that within a release build, they are not. This is one of the main reasons why people complain that their release build doesn't work. Take the following example: bool bIsEqual; if ( ... ) bIsEqual = true; if ( bIsEqual ) // Is this true or false? in debug it was initialized to false ....

          R 1 Reply Last reply
          0
          • W Waldermort

            In a debug build, variables are initialized, but you will find that within a release build, they are not. This is one of the main reasons why people complain that their release build doesn't work. Take the following example: bool bIsEqual; if ( ... ) bIsEqual = true; if ( bIsEqual ) // Is this true or false? in debug it was initialized to false ....

            R Offline
            R Offline
            rajeevktripathi
            wrote on last edited by
            #5

            Hi could not understand your example I wrote an example As follows: int pg; int _tmain(int argc, _TCHAR* argv[]) { int pl; int s; printf("Global %d \n", pg); printf("Local %d \n" , pl); scanf("%d", &s); return 0; } and while printing pl prompts an error message "Run-Time Check Failure #3 - The variable 'pl' is being used without being defined." any comments on this....

            J 1 Reply Last reply
            0
            • R rajeevktripathi

              Hi All I have a confusion related to global and local variables... why global variables are initialized to zero whereas local variables are not... What is the reason behind this. Please suggest me the answer. Thanks & Regards

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #6

              rajeevktripathi wrote:

              why global variables are initialized to zero whereas local variables are not...

              During the CRT initialization routine ( wWinMainCRTStartup function ), there is special code for intializing the c objects and c++ objects. You can find a function called _initterm() called from the wWinMainCRTStartup. That function does all this.

              nave [OpenedFileFinder]

              1 Reply Last reply
              0
              • R rajeevktripathi

                Hi could not understand your example I wrote an example As follows: int pg; int _tmain(int argc, _TCHAR* argv[]) { int pl; int s; printf("Global %d \n", pg); printf("Local %d \n" , pl); scanf("%d", &s); return 0; } and while printing pl prompts an error message "Run-Time Check Failure #3 - The variable 'pl' is being used without being defined." any comments on this....

                J Offline
                J Offline
                jhwurmbach
                wrote on last edited by
                #7

                rajeevktripathi wrote:

                "Run-Time Check Failure #3 - The variable 'pl' is being used without being defined."

                At compile time, you have got a "warning C4700: local variable 'pl' used without having been initialized". As you have enabled "Runtime checks" (probably a Debug-build), the runtime complains that you use this variable without prior assignment to it.


                Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
                George Orwell, "Keep the Aspidistra Flying", Opening words

                1 Reply Last reply
                0
                • R rajeevktripathi

                  Hi All I have a confusion related to global and local variables... why global variables are initialized to zero whereas local variables are not... What is the reason behind this. Please suggest me the answer. Thanks & Regards

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  rajeevktripathi wrote:

                  why global variables are initialized to zero whereas local variables are not...

                  You'll likely see a difference in initialization between release and debug compiles of your program. In debug mode, since the frame pointer is always pushed onto the stack at routine entry, variables are almost always assigned locations on the stack (e.g., 0). In the release mode, optimizations of the compiler (and other factors) may detect that the frame pointer is not needed, so the frame pointer is not pushed onto the stack, thus no initial value.


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  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