C++ expression evaluation.
-
Hello, consider the following program
void main()
{
int tr = 0;
int rows = 256;
int ms = 3;
tr += (rows & (1 << (ms - 1))) << (ms - 1);
tr = rows & (~(1 << ms));
}in above program have used only 3 integers...so can we consider that this program's memory requirement will be 3 integers (3*4 bytes). or the compiler will generate some intermediate integers for storing values of expressions like (~(1 << ms)) etc... and thus the memory requirements of this program will be more than 3 integers. i know it all depends upon how the compiler generates the code. but wanted to know the relation (w.r.t memory) between the variables declared and the intermediate expressions. Or you may please direct me to appropriate resources (forums, links)for this.. Thanks.
-
Hello, consider the following program
void main()
{
int tr = 0;
int rows = 256;
int ms = 3;
tr += (rows & (1 << (ms - 1))) << (ms - 1);
tr = rows & (~(1 << ms));
}in above program have used only 3 integers...so can we consider that this program's memory requirement will be 3 integers (3*4 bytes). or the compiler will generate some intermediate integers for storing values of expressions like (~(1 << ms)) etc... and thus the memory requirements of this program will be more than 3 integers. i know it all depends upon how the compiler generates the code. but wanted to know the relation (w.r.t memory) between the variables declared and the intermediate expressions. Or you may please direct me to appropriate resources (forums, links)for this.. Thanks.
If you are using Visual Studio then you have the opportunity to see the disassembly listing (for instance via the debugger or the /FA compiler option). Other compilers may provide you this facility as well.
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] -
Hello, consider the following program
void main()
{
int tr = 0;
int rows = 256;
int ms = 3;
tr += (rows & (1 << (ms - 1))) << (ms - 1);
tr = rows & (~(1 << ms));
}in above program have used only 3 integers...so can we consider that this program's memory requirement will be 3 integers (3*4 bytes). or the compiler will generate some intermediate integers for storing values of expressions like (~(1 << ms)) etc... and thus the memory requirements of this program will be more than 3 integers. i know it all depends upon how the compiler generates the code. but wanted to know the relation (w.r.t memory) between the variables declared and the intermediate expressions. Or you may please direct me to appropriate resources (forums, links)for this.. Thanks.
It depends on the compiler, and the switch settings you apply. A smart compiler may toss it all out as all calculations ultimately generate a value for tr which is then not used at all. And if you were to add a function call (say
print(tr);
) which makes the calculations necessary, the compiler could still discover that the tr value is a constant that can be evaluated right away at compile-time, hence tossing the other variables. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.4 -
Hello, consider the following program
void main()
{
int tr = 0;
int rows = 256;
int ms = 3;
tr += (rows & (1 << (ms - 1))) << (ms - 1);
tr = rows & (~(1 << ms));
}in above program have used only 3 integers...so can we consider that this program's memory requirement will be 3 integers (3*4 bytes). or the compiler will generate some intermediate integers for storing values of expressions like (~(1 << ms)) etc... and thus the memory requirements of this program will be more than 3 integers. i know it all depends upon how the compiler generates the code. but wanted to know the relation (w.r.t memory) between the variables declared and the intermediate expressions. Or you may please direct me to appropriate resources (forums, links)for this.. Thanks.
thanks..
-
Hello, consider the following program
void main()
{
int tr = 0;
int rows = 256;
int ms = 3;
tr += (rows & (1 << (ms - 1))) << (ms - 1);
tr = rows & (~(1 << ms));
}in above program have used only 3 integers...so can we consider that this program's memory requirement will be 3 integers (3*4 bytes). or the compiler will generate some intermediate integers for storing values of expressions like (~(1 << ms)) etc... and thus the memory requirements of this program will be more than 3 integers. i know it all depends upon how the compiler generates the code. but wanted to know the relation (w.r.t memory) between the variables declared and the intermediate expressions. Or you may please direct me to appropriate resources (forums, links)for this.. Thanks.
In addition to previous replies, since the integers are automatic, i.e. allocated on the stack, which already has storage reserved, the memory footprint will not be affected if you add or remove such local variables, as it does not affect system resources. However, the stack will fill up quicker if you (or the compiler) add them.