Use C++17 features to better delete all pointers from a container

c++
c compiler
pointers in c
do people use c++ anymore
c vs c++
do developers still use c
use c struct in c++
how to use c code in c++

Before C++17, deleting all pointers from a map looked like:

for (TMapBuffOnAttrs::iterator it = m_map_buff_on_attrs.begin();  it != m_map_buff_on_attrs.end(); it++)
{
    if (NULL != it->second)
    {
        delete(it->second);
    }
}
m_map_buff_on_attrs.clear();

With C++17, we got:

for (auto it = m_map_buff_on_attrs.begin();  it != m_map_buff_on_attrs.end(); it++)
{
    if (NULL != it->second)
    {
        delete(it->second);
    }
}

Is there a simpler solution?

Yes.

for (auto it = m_map_buff_on_attrs.begin();  it != m_map_buff_on_attrs.end(); it++)

Since you only use the value *it and no other data from that iterator, a for-range loop would be simpler.

if (NULL != it->second)
{
    delete(it->second);
}

Deleting NULL is well-defined and has no effect, this is a test you can skip.


Which gives:

for (auto& p : m_map_buff_on_attrs) {
    delete p.second;
}

Manipulating raw pointers is error-prone and makes you write more useless code. If m_map_buff_on_attrs were a map of smart pointers, your code would simply be:

} // m_map_buff_on_attrs goes out of scope and destroys and frees everything.

When to use C over C++, and C++ over C?, You pick C when. you need portable assembler (which is what C is, really) for whatever reason,; your platform doesn't provide C++ (a C� In this article, let us discuss how to debug a c program using gdb debugger in 6 simple steps. Write a sample C program with errors for debugging purpose. To learn C program debugging, let us create the following C program that calculates and prints the factorial of a number. However this C program contains some errors in it for our debugging

RAII pattern is your friend:

using TMapBuffOnAttrs = std::map<std::string, std::unique_ptr<Attr>>;

m_map_buff_on_attrs["attrName"] = std::make_unique<Attr>(x, a);


....
// loop is not needed, to delete just do:
m_map_buff_on_attrs.clear();

How to Use the ?: Decision-Making Function in C Programming , Perhaps the most cryptic of the decision-making tools in C, ?: is a favorite of programmers who enjoy obfuscating their code. It generates results based on if- true� Disclaimer. All content on this website, including dictionary, thesaurus, literature, geography, and other reference data is for informational purposes only.

You can use for_each with lambda function.

Here is a way:

std::for_each(m_map_buff_on_attrs.begin(), m_map_buff_on_attrs.end(), 
              [](auto &item) {delete item.second;}); 

How to use C code in C++, Yes, you can include C headers in C++ code. It's normal to add this: #ifdef __ cplusplus extern "C" { #endif // C header here #ifdef __cplusplus }� United Security (USEC), is a private military company (PMC) and one of the main factions in Escape from Tarkov.USEC is employed by the notorious Terra Group international corporation, vigorously engages in armed clashes, hindering the investigation of Terra Group activities, carried on by local authorities.

Yes, use smartpointers. And simply 'clear()' the container... Otherwise this is just "C code written in C++17"

C++ programming with Visual Studio Code, You will need to install these tools or use those already installed on your computer. You can find more documentation on using the Microsoft C/C++ extension� Using C$ To Browse The C Drive Over The Network. - posted in Tips and Tricks: I don't know if somebody already posted about this , but i thought i would because it's beeing much helpfull at work

The C Programming Language, C is a very powerful and widely used language. It is used in many scientific programming situations. It forms (or is the basis for) the core of the modern languages Java and C++. It allows you access to the bare bones of your computer. using-directives for namespaces and using-declarations for namespace members using-declarations for class members type alias and alias template declaration (since C++11)

Why do we still use C language?, Primary reason for using C language is its easy to build a programming foundation when its taught using C. Professionally, C is used for writing kernel modules,� c/o ACME Company, Inc. 123 Street St. City, ST 99999 . Hotel Example. You can also use it to send a letter to someone staying at a hotel if you don’t know their room number. Jane Doe c/o Westin Hotel 123 Street St. City, ST 99999 . Event Invitation Example. Imagine you are sending invitations to an event and don’t know an invitee’s (John

What is C?, That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use. C has� Top-Rated Use C. Free 2-Day Shipping with Amazon Prime.

Comments
  • Range-based for loop? std::for_each? Smart pointers? Not using pointers at all?
  • delete handles null properly, so if is unnecessary. () after delete are unnecessary as well.
  • Foregoing the raw pointers could simplify quite a bit. Just saying.
  • Put std::unique_ptr<type> as the value in the map and the things will be deleted automatically. Another way: for(auto& [key, value] : m_map_buff_on_attrs) delete value;
  • @Tamás Sipos You could get way better and more simplified answers if you would mind to show us what TMapBuffOnAttrs actually is.
  • Thank you! what is the difference between delete and clear? Missed the ".clear()" from the code!
  • @TamásSipos clear() on a container destroys the contained elements. In the case of raw pointers, only the pointer is destroyed, not what it points to. delete on a pointer does that. With a container of raw pointers, you need both. On a container of smart pointers though, clear() is enough since the destructor of the smart pointers destroyes the pointee.
  • Thanks for the reply!
  • yes it is. In general it is recommended to use RAII pattern with anything. You may need to take a look on that.
  • One more thing, everything here is C++11, only std::make_unique is C++ 14.
  • Thanks for the reply!
  • best smart pointer is the simplest smart pointer that suits your needs. It could be unique_ptr but you need to master move owhership or simply shared_ptr.
  • Thanks for the reply!