C ++ dynamic memory details

I am a C and Java programmer, so memory allocation and OOP are not new to me. But I'm not sure how to avoid memory leaks when implementing objects in C ++. Namely:

string s1("0123456789"); string s2 = s1.substr(0,3); 

s2 now has a new string object, so it should be freed with:

 delete &s2; 

Right?

In addition, can I assume that I will need to remove the address for any (new) object returned by the function, regardless of the type of the return value, which is not a pointer or reference? It seems strange that an object living on the heap will not be returned as a pointer when it should be freed.

+4
source share
8 answers

No,

Both s1 and s2 will be destroyed if they are not in scope.

s1.substr() will create a temporary object that you do not need to think about.

+12
source

No.

You only need to free the allocated memory (i.e. via new or memalloc).

+16
source

There are several levels in the answers.

First, variables can be declared in several ways:

  • As local variables (inside a function or as members of a class). Examples: int i or int i = 42 . They have an automatic storage duration (these examples are technically shortened for auto int i or auto int i = 42 , although the auto keyword is almost never used. This means that these variables will be automatically freed when they go out of scope. Their destructor is guaranteed will be called (regardless of how you leave the scope, whether the function is returned or throws an exception, their destructor will be called when they leave the scope, and then the freed memory will be rim) local variables declared in this way are allocated on the stack.
  • Static variables (with the static implying a static storage duration, unlike the automatic one shown above). They simply remain throughout the program and therefore do not need to be freed.
  • on the heap, with dynamic allocation ( new int or new int(42) ). They need to be freed manually by calling delete .

So, at the lowest level, you just need to maintain symmetry. If something was highlighted using new , release it with delete, malloc is freed by free , and new [] by delete [] `. And variables declared without any of them are automatically processed and should not be manually released.

Now, to simplify memory management, RAII is usually used. The method is based on the observation that only dynamically allocated objects should be manually released, and local variables give you a very convenient hook for implementing custom actions when a local variable goes out of scope.

So, dynamic allocation is completed inside a separate class, which can be allocated as a local object on the stack. He can create any dynamic distributions that you need in his constructor, and his destructor clears it with the necessary delete calls.

This means that you essentially never need to call delete in the top-level code. It will almost always be hidden behind the destructor of the RAII object. new also become rare, but are still used with smart pointers (for example: boost::shared_ptr<int>(new int(42)) , which dynamically allocates an integer and then passes it to the smart pointer, which takes care of his possession, and automatically cleans it.

+8
source

Both s1 and s2 are automatically allocated. You do not delete them. You only delete objects you created with new .

C ++ knows three distribution modes: automatic, static, and dynamic. Read them. Automatic values, such as your lines in the example, are automatically freed when they leave the scope. Their destructor is called automatically. Any memory that strings dynamically allocated during their operation is freed up when the row destructor is called.

+5
source

Like everyone else, deletion is not required here. If you do not see new , you (as a rule) do not need delete . I want to add that s2 does not have a new string object. s2 - string object from the moment of its declaration. Assigning a slice from s1 to s2 changes s2 so that it contains the same characters as in the substring.

It is very important to understand what is happening here, and it will become even more important the more you dig in C ++. Oh, and congratulations on learning a new language.

+3
source

Why do you need to download s2 for free? dynamic memory deletion in s2 will be performed by the std :: string destructor.

+1
source

No, s2 does not need to be deleted manually. This is a local variable on the stack that will be automatically destroyed after it leaves the scope, just as it was automatically allocated when it was declared. Usually you only delete things that you have allocated with new .

Temporary objects returned by functions are automatically controlled and destroyed at the end of the instruction - if they are needed longer, they are usually copied to a local variable until temporary destruction (for example, by simply assigning the Object o = f(); type Object o = f(); or as in a line with substr() in your example).

If the function returns a pointer, an object with a pointer is not automatically controlled in this way. The documentation should note who is responsible for the removal of the object after completion of all work. As a standard agreement, the one who selected the object is usually responsible for its removal, but the details need to be documented somewhere.

+1
source

One of the most important concepts to understand in modern C ++ (especially from the c-background) is RAII . C ++ encapsulates resources such as memory (or mutexes or transactions) within classes that β€œacquire” a resource during construction (the string constructor allocates dynamic memory) and β€œfree” it upon destruction (destruction of the row class frees it). Since the destruction of stack objects is deterministic (the stack based object expires when the scope is closed), the release does not need to be recorded and will occur even if an exception is thrown.

So no. In most of my codes, I never write explicit delete (or delete []), because either the resources are controlled by a string, or STL-conatiner, or shared_ptr or scoped_ptr.

+1
source

All Articles