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. variables

variables

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
21 Posts 7 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.
  • K khomeyni

    in the name of god hello when we declare a variable in c++ as : int x; int x1=98; then initialize it with a value. how we can know that a variable has been initialized or no?is there any way (compiler can or ...)to distinguish between initialized variable and non-ini... that we dont know the value of them. valhamdolelah.

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #4

    khomeyni wrote:

    how we can know that a variable has been initialized or no?

    Looking at the code? Ah, and the compiler warns about uninitialized variables. :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    In testa che avete, signor di Ceprano?

    K 1 Reply Last reply
    0
    • M Maximilien

      nope. (don't take my word for it) in DEBUG more, variable _can_ be initialized to zero (or some other default patterns) as an aid for internal debugging , but in RELEASE, they are not (for performance issues). When compiling, I would assume the compiler is just checking if the variable was assigned something.

      This signature was proudly tested on animals.

      K Offline
      K Offline
      khomeyni
      wrote on last edited by
      #5

      so how compiler knows it?

      M 1 Reply Last reply
      0
      • CPalliniC CPallini

        khomeyni wrote:

        how we can know that a variable has been initialized or no?

        Looking at the code? Ah, and the compiler warns about uninitialized variables. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        K Offline
        K Offline
        khomeyni
        wrote on last edited by
        #6

        thanks but: suppose i created a variable x:int x; and then during program i dont use it and at last i want to know if it is initialized or no?for example if a condition occured then it would be initialized else ... and i dont know anything about this condition. i can use some idea as initializing it by infinite value but i want to know how to compiler do it? or is another way for? valhamdolelah.

        CPalliniC D J 3 Replies Last reply
        0
        • K khomeyni

          thanks but: suppose i created a variable x:int x; and then during program i dont use it and at last i want to know if it is initialized or no?for example if a condition occured then it would be initialized else ... and i dont know anything about this condition. i can use some idea as initializing it by infinite value but i want to know how to compiler do it? or is another way for? valhamdolelah.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #7

          You should initialize a variable as soon as possible in your program. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • K khomeyni

            so how compiler knows it?

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #8

            it could checks syntactically if the variable is on the left-hand side of an '=', of if syntactically if follows the syntax of an initializer... for example :

            int i;
            i = 0;

            or

            int i(123);

            or

            class Tata
            {
            Tata() : m_i(123){};

            int m_i;
            };

            Or there might be other more advanced compiler tricks to keep track of variable assignments...

            This signature was proudly tested on animals.

            K 1 Reply Last reply
            0
            • K khomeyni

              thanks but: suppose i created a variable x:int x; and then during program i dont use it and at last i want to know if it is initialized or no?for example if a condition occured then it would be initialized else ... and i dont know anything about this condition. i can use some idea as initializing it by infinite value but i want to know how to compiler do it? or is another way for? valhamdolelah.

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

              If no '=' has been encountered for a given variable during the token-parsing phase, it is assumed to be uninitialized. This is covered in compiler classes, and books such as the dragon book. Keep in mind, though, all variables will have a value regardless of what that value is or where it came from.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              K 1 Reply Last reply
              0
              • K khomeyni

                thanks but: suppose i created a variable x:int x; and then during program i dont use it and at last i want to know if it is initialized or no?for example if a condition occured then it would be initialized else ... and i dont know anything about this condition. i can use some idea as initializing it by infinite value but i want to know how to compiler do it? or is another way for? valhamdolelah.

                J Offline
                J Offline
                josda1000
                wrote on last edited by
                #10

                How about this. Use a boolean along with the integer. for example: int x; bool xIsInitialized = false; And then, when you do start assigning some value to x, update the boolean to true. I think this is a big waste of time though. You should always have it initialized, possibly to -1 or 0 to show that it hasn't been assigned to yet.

                M CPalliniC 2 Replies Last reply
                0
                • K khomeyni

                  in the name of god hello when we declare a variable in c++ as : int x; int x1=98; then initialize it with a value. how we can know that a variable has been initialized or no?is there any way (compiler can or ...)to distinguish between initialized variable and non-ini... that we dont know the value of them. valhamdolelah.

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #11

                  You should change your way of thinking: always initialize your variables. You should never have to test if a variable has been initialized or not.

                  Cédric Moonen Software developer
                  Charting control [v2.0] OpenGL game tutorial in C++

                  K 1 Reply Last reply
                  0
                  • J josda1000

                    How about this. Use a boolean along with the integer. for example: int x; bool xIsInitialized = false; And then, when you do start assigning some value to x, update the boolean to true. I think this is a big waste of time though. You should always have it initialized, possibly to -1 or 0 to show that it hasn't been assigned to yet.

                    M Offline
                    M Offline
                    Maximilien
                    wrote on last edited by
                    #12

                    voted you down... 1- bad programming practice. 2- twice the danger of doing something stupid .. (change one and forget to change the other)

                    This signature was proudly tested on animals.

                    J 1 Reply Last reply
                    0
                    • M Maximilien

                      voted you down... 1- bad programming practice. 2- twice the danger of doing something stupid .. (change one and forget to change the other)

                      This signature was proudly tested on animals.

                      J Offline
                      J Offline
                      josda1000
                      wrote on last edited by
                      #13

                      Um. Yeah. I know. That's why I didn't suggest it. It was an idea. If you read my second line, I said I didn't suggest it. Next time, make sure you read my whole statement.

                      1 Reply Last reply
                      0
                      • M Maximilien

                        it could checks syntactically if the variable is on the left-hand side of an '=', of if syntactically if follows the syntax of an initializer... for example :

                        int i;
                        i = 0;

                        or

                        int i(123);

                        or

                        class Tata
                        {
                        Tata() : m_i(123){};

                        int m_i;
                        };

                        Or there might be other more advanced compiler tricks to keep track of variable assignments...

                        This signature was proudly tested on animals.

                        K Offline
                        K Offline
                        khomeyni
                        wrote on last edited by
                        #14

                        in the name of god thanks Maximilien. i don't know it up to now. i have also another question :you said that when we use = compiler knows that a variable is initialized so if it save any data for this?any data to know which variables are initialized if yes where? thanks. valhamdolelah.

                        D 1 Reply Last reply
                        0
                        • K khomeyni

                          in the name of god thanks Maximilien. i don't know it up to now. i have also another question :you said that when we use = compiler knows that a variable is initialized so if it save any data for this?any data to know which variables are initialized if yes where? thanks. valhamdolelah.

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

                          khomeyni wrote:

                          i don't know it up to now.

                          Don't know what?

                          khomeyni wrote:

                          i have also another question :you said that when we use = compiler knows that a variable is initialized so if it save any data for this?any data to know which variables are initialized if yes where?

                          The information you are after is not available via code. It's something that the compiler handles internally.

                          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          K 1 Reply Last reply
                          0
                          • D David Crow

                            If no '=' has been encountered for a given variable during the token-parsing phase, it is assumed to be uninitialized. This is covered in compiler classes, and books such as the dragon book. Keep in mind, though, all variables will have a value regardless of what that value is or where it came from.

                            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                            K Offline
                            K Offline
                            khomeyni
                            wrote on last edited by
                            #16

                            hello how it saves that a variable is initialized or no.does it saves any data(where?)?or compiler uses a special method? valhamdolelah.

                            D CPalliniC 2 Replies Last reply
                            0
                            • D David Crow

                              khomeyni wrote:

                              i don't know it up to now.

                              Don't know what?

                              khomeyni wrote:

                              i have also another question :you said that when we use = compiler knows that a variable is initialized so if it save any data for this?any data to know which variables are initialized if yes where?

                              The information you are after is not available via code. It's something that the compiler handles internally.

                              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                              K Offline
                              K Offline
                              khomeyni
                              wrote on last edited by
                              #17

                              i don't care about = ,() , ... thanks.but indeed i want to know more ,but as it seem it is deep because of compilers. thanks. valhamdolelah.

                              1 Reply Last reply
                              0
                              • K khomeyni

                                hello how it saves that a variable is initialized or no.does it saves any data(where?)?or compiler uses a special method? valhamdolelah.

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

                                This is not a basic programming subject, and certainly does not belong in the C/C++ forum. Unless you've written a compiler/parser, or have studied the subject, any answer that you are provided will not make any sense.

                                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                1 Reply Last reply
                                0
                                • K khomeyni

                                  hello how it saves that a variable is initialized or no.does it saves any data(where?)?or compiler uses a special method? valhamdolelah.

                                  CPalliniC Offline
                                  CPalliniC Offline
                                  CPallini
                                  wrote on last edited by
                                  #19

                                  Inner details of compilers are secret! :suss: On the serious side: inner details of compilers are secret! :rolleyes: If you are still there, consider that compiler's developers may freely choose the implementation details, provided they fullfill the language specification requirements. Hence different compilers may use different strategies to deal with the same problem. If you are really interested in compiler inner mechanisms, I would suggest to start with some introductory material as, for instance, the excellent classic Wirth's "Compiler Construction", freely available (for instance here [^]). As further reading, you may consider Hanson & Fraser's book "A Retargetable C Compiler: Design and Implementation" (see [^]): since lcc is a real compiler and its sourc code is available, you may have a look at it (well, gcc source code too is available...If you are going to examine it, good luck!). :)

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                  [My articles]

                                  In testa che avete, signor di Ceprano?

                                  1 Reply Last reply
                                  0
                                  • J josda1000

                                    How about this. Use a boolean along with the integer. for example: int x; bool xIsInitialized = false; And then, when you do start assigning some value to x, update the boolean to true. I think this is a big waste of time though. You should always have it initialized, possibly to -1 or 0 to show that it hasn't been assigned to yet.

                                    CPalliniC Offline
                                    CPalliniC Offline
                                    CPallini
                                    wrote on last edited by
                                    #20

                                    josda1000 wrote:

                                    How about this. Use a boolean along with the integer. for example: int x; bool xIsInitialized = false; And then, when you do start assigning some value to x, update the boolean to true.

                                    As it stands it is just an abomination. :)

                                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    In testa che avete, signor di Ceprano?

                                    1 Reply Last reply
                                    0
                                    • C Cedric Moonen

                                      You should change your way of thinking: always initialize your variables. You should never have to test if a variable has been initialized or not.

                                      Cédric Moonen Software developer
                                      Charting control [v2.0] OpenGL game tutorial in C++

                                      K Offline
                                      K Offline
                                      khomeyni
                                      wrote on last edited by
                                      #21

                                      in the name of god thanks everybody. i find my answer and thanks for everything. i promise you to don't question any other question that is irrelevant. thanks. valhamdolelah.

                                      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