Defining variables in blocks of control structures

If I define a variable inside a control structure block, does it exist only when this control structure block is executed, and not in the entire execution of the closing function?

Also, how can I track the exact memory usage of my programs and its changes (i.e.: changes in memory usage by creating and deleting variables)?

added later: in the following code, I know that v scope is a block, but I want to know if v will be created / destroyed in memory at the beginning / end of the if block or at the beginning / end of the func function?

void func () { if (true) { int v;//automatic storage class v = 1; } } 
+4
source share
3 answers

If I define a variable inside a control structure block, does it exist only when this control structure block is executed, and not in the entire execution of the closing function?

It depends on where you declare the define variable.

A variable is only available in the area where you declare it. It may be available outside the scope if you explicitly pass it, but if it remains valid, it will depend on the type of storage of the variable.
For example, static variables are preserved throughout the entire program life cycle, and the return address of an automatic variable from a function will lead to Undefined Behavior, because the variable will not remain valid after the function returns.

Good Reading: What is the difference between a definition and a declaration?

How can I control the exact memory usage of my programs and its changes (i.e.: changes in memory usage by creating and deleting variables)?

I believe that you will want to get information about dynamically distributed objects, because automatic objects live long enough in their area, they will be automatically destroyed, so they usually do not cause any problems.

For dynamic objects, you can use memory profiling tools such as valgrind with Massif , or you could replace the new and delete statements for your class and collect diagnostic information.


EDIT: To refer to the updated Q.

In the following code, I know that region v is an if block, but I want to know if v will be created / destroyed in memory at the beginning / end of the if block or at the beginning / end of the func function?

v is created when the region in which it is declared begins and the statement in which it is declared is executed. v destroyed upon completion of the scope ie } .
This concept is used to form the basis of one of the most widely used concepts in C ++, known as Resource Initialization (RAII) . Every C ++ programmer must be aware of this.

It’s easier to show and verify the creation and destruction of objects using this small modified example.

 #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; } 

Conclusion:

Before calling func ()
Before You Start Scope | In Myclass Constructor
In Myclass Destructor
At the end of the transaction After calling the func () function

+2
source

A local variable is limited in scope to all code below the declaration until the end of the closing block. The variable is also visible to any other blocks that are enclosed in the source block.

Additional Information

0
source

If the local variable is an object, then this lifetime of the object 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) 
0
source

Source: https://habr.com/ru/post/1413401/


All Articles