defining variables in blocks of control structures
If I define a variable inside the block of a control structure, does it exist only in the execution of that control structure's block and not in the whole execution of the enclosing function?
also how can I monitor the exact memory usage of my programs and its changes (ie: changes in memory usage by creating and destroying variables)?
added later: in following code I know v scope is if block, but I want to know whether v is created/destroyed in the memory at the start/end of the if block or at the start/end of the function func?
void func ()
{
if (true)
{
int v;//automatic storage class
v = 1;
}
}
If I define a variable inside the block of a control structure, does it exist only in the execution of that control structure's block and not in the whole execution of the enclosing function?
It depends on where you declare the variable not define it.
The variable is only accessible in the scope where you declare it. It can be accessed beyond tht scope if you explicitly pass it around but if it remains valid would depend on the storage type of the variable.
For eg: static
variables remain alive throughout the program lifetime, while,
returning address of an automatic variable from an function would result in Undefined Behavior because the variable does not remain valid after the function returns.
Good Read: What is the difference between a definition and a declaration?
How can I monitor the exact memory usage of my programs and its changes (ie: changes in memory usage by creating and destroying variables)?
I believe you would want to get information on dynamically allocated objects because automatic objects only live long enough in their scope, they will automagically destroyed so they don't usually cause any problems.
For dynamic objects You can use memory profiling tools like valgrind with Massif or you could replace new
and delete
operators for your class and collect the diagnostic information.
EDIT: To address the updated Q.
In following code I know v
scope is if
block, but I want to know whether v
is created/destroyed in the memory at the start/end of the if block or at the start/end of the function func
?
v
is created when the scope in which it is declared starts and the statement where it is declared gets executed. v
is destroyed once the scope ends ie }
is reached.
This concept is used for forming the basis of one of the most widely used concepts in C++ known as Resource Allocation is Initialization(RAII) . Every C++ programmer absolutely must know about it.
It is simple to demonstrate and verify the creation and destruction of the objects through this small modified code sample :
#include<iostream>
class Myclass
{
public:
Myclass(){std::cout<<"nIn Myclass Constructor ";}
~Myclass(){std::cout<<"nIn Myclass Destructor";}
};
void func()
{
std::cout<<"nBefore Scope Begins";
if (true)
{
Myclass obj;//automatic storage class
}
std::cout<<"nAfter Scope Ends";
}
int main()
{
std::cout<<"nBefore Calling func()";
func();
std::cout<<"nAfter Calling func()";
return 0;
}
The output is:
Before Calling func()
Before Scope Begins
In Myclass Constructor
In Myclass Destructor
After Scope Ends
After Calling func()
A local variable is limited in scope to all the code below the declaration until the end of the enclosing block. The variable is also visible to any other blocks that are enclosed in the original block.
More info
If a local variable is an object, then that object's lifetime ends at the end of the block in which the variable was declared. Example:
Foo x; // lifetime of "x" begins
{
some_stuff();
Foo y; // lifetime of "y" begins
Foo & a = x; // local variable "a", no new object
more_stuff();
} // lifetime of "y" ends; "y" and "a" go out of scope
// (lifetime of "x" ends with its surrounding scope,
// or end of program if it was global)
链接地址: http://www.djcxy.com/p/73044.html
上一篇: 自动源代码替换
下一篇: 在控制结构块中定义变量