C ++ - What does "Stack automatic" mean?

In my browsers among the Internet, I came across this post , which includes this

"(Well written) C ++ is great at lengths for automatically creating a stack. Objects work just like primitives, as reflected in Stroustrap's recommendation to" do as ints do. "This requires much greater adherence to the principles of object-oriented development: your class is not right until it "works" like "int", following the "Rule of Three", which guarantees it (like int) to be created, copied and correctly destroyed as an automatic stack. "

I made a little C and C ++ code, but only in passing, never serious, but I'm just wondering what that means?

Can someone give an example?

+4
source share
5 answers

Stack objects are processed automatically by the compiler.

When a scope is left, it is deleted.

{ obj a; } // a is destroyed here 

When you do the same with the β€œnew” object, you get a memory leak:

 { obj* b = new obj; } 

b is not destroyed, so we have lost the ability to recover memory b. And maybe worse, the object cannot clean itself.

The following is common in C:

 { FILE* pF = fopen( ... ); // ... do sth with pF fclose( pF ); } 

In C ++ we write the following:

 { std::fstream f( ... ); // do sth with f } // here f gets auto magically destroyed and the destructor frees the file 

When we forget to call fclose in Example C, the file is not closed and cannot be used by other programs. (for example, it cannot be deleted).

Another example demonstrating a line of an object that can be built, assigned, and which is destroyed when leaving the area.

 { string v( "bob" ); string k; v = k // v now contains "bob" } // v + k are destroyed here, and any memory used by v + k is freed 
+12
source

In addition to other answers:

C ++ actually has the auto keyword to explicitly declare an object storage class. Of course, this is completely useless because it is an implied storage class for local variables and cannot be used anywhere. The opposite of auto is static (both locally and globally).

The following two declarations are equivalent:

 int main() { int a; auto int b; } 

Since the keyword is absolutely useless, it will actually be reworked in the following C ++ standard ("C ++ 0x") and will receive a new value, namely: it allows the compiler to infer the type of a variable from its initialization (for example, var in C #):

 auto a = std::max(1.0, 4.0); // `a` now has type double. 
+2
source

Variables in C ++ can be declared on the stack or on the heap. When you declare a variable in C ++, it automatically goes onto the stack unless you explicitly use the new operator (it goes into the heap).

 MyObject x = MyObject(params); // onto the stack MyObject * y = new MyObject(params); // onto the heap 

This greatly affects the way memory is managed. When a variable is declared on the stack, it will be freed when it goes out of scope. The variable in the heap will not be destroyed until the deletion of the object is explicitly called.

+1
source

An automatic stack is variables that are pushed onto the stack of the current method. The idea of ​​creating a class that can act as an automatic stack is that it can be fully initialized with one call and destroyed by another. It is very important that the destructor free all resources allocated by the object, and its constructor returns an object that has been fully initialized and ready for use. Similarly, for the copy operation, the class should be easily implemented, fully functional and independent.

Using such a class should be similar to how primitive int, float, etc. are used. You define them (in the end, give them some initial value), and then pass them in, and finally leave the compiler to clear.

+1
source

Correct me if I am wrong, but I think that the copy operation is not necessary for the full use of automatic stack cleanup. For example, consider a classic MutexGuard object; it does not need a copy operation to be useful as a stack automatically, or is it?

+1
source

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


All Articles