variables
-
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.
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.
-
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.
-
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.
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.
-
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.
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
-
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
-
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
-
hello how it saves that a variable is initialized or no.does it saves any data(where?)?or compiler uses a special method? valhamdolelah.
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
-
hello how it saves that a variable is initialized or no.does it saves any data(where?)?or compiler uses a special method? valhamdolelah.
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] -
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.
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] -
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++