1

Let's say I have a struct defined as so:

struct Barre {
        int startString;
        int endString;

        Barre() { startString = endString = -1; }
        Barre(int s, int e) : startString(s), endString(e) {}
        bool exists() { return startString > -1; }
        };

I will create an instance of this struct like this, for example:

Barre b = Barre(2, 4);

Let's say I insert this into a std::map<int, Barre> which is a member of a class, with a key of, for example, 3.

If I then create another Barre as above and overwrite the value of the map at key 3 with this new instance of the Barre struct, do I need to explicitly delete the old Barre object that I'm overwriting to prevent memory leaks? Or does it not persist once it is no longer stored in a map in this way?

Thanks for any help.

  • If you don't allocate them with new or new[] you don't use delete or delete[] – drescherjm Mar 07 '19 at 17:19
  • "Should I explicitly delete structs in C++?" - You should `delete` what you `new`. If you don't `new` something, `delete`ing it is a bug. But please, try to stay far away from manual memory management in the first place. – Jesper Juhl Mar 07 '19 at 17:20
  • The very simple rule to remember is: If you do not use `new`/`new[]`, you do not need to use `delete`/`delete[]`. – NathanOliver Mar 07 '19 at 17:20
  • @JThistle _If you do not use ..._ means directly but also indirectly, you can use lib doing the allocation but letting you the responsibility to do the _delete_, exactly like for _malloc/free_ – bruno Mar 07 '19 at 17:23
  • 1
    Also, as a rule of thumb for modern C++ (C++11 and later), you really shouldn't ever need to use `new` and especially not `delete` yourself, unless you are doing some pretty deep library development, or use some existing framework which requires you to use them for legacy reasons (for example with Qt you'll use `new`, but don't need `delete`, and C++11 doesn't yet have `make_unique` function). Instead, use smart pointers. – hyde Mar 07 '19 at 17:25
  • 3
    @JThistle _"it's worth noting that I have searched for an answer to this and can't find anything."_ Well, [this](https://www.google.com/search?q=c%2B%2B+when+do+i+need+delete+site:stackoverflow.com&rlz=1C1CHBF_deDE833DE833&sa=X&ved=2ahUKEwiDi83kxPDgAhUtMuwKHarFCmQQrQIoBDAKegQICRAM&biw=1600&bih=789) was the google query I used to find the duplicate questions: _c++ when do i need delete site:stackoverflow.com_ – πάντα ῥεῖ Mar 07 '19 at 17:27
  • 1
    I used 'do I need to delete a struct' and variations on that, DuckDuckGo returned nothing helpful. –  Mar 07 '19 at 17:30
  • 2
    @JThistle The problem with your query might possibly have been that you searched for _`struct`_ specifically, which is completely irrelevant for the question. A `struct` is just the same as a `class` with all members `public` as default. Nothing to do with dynamic memory management at all. – πάντα ῥεῖ Mar 07 '19 at 17:36
  • 2
    @πάνταῥεῖ When reading questions, it's good to keep in mind that things that are self-evident for someone who knows a language, necessarily aren't that for everybody, even experienced programmers. For example a C# developer (I don't know if OP is) entering C++ world might have a bit of WTF moment when they learn that `struct` and `class` are basically the same thing, and until that realization might have trouble formulating a suitable search engine query. – hyde Mar 07 '19 at 18:23

0 Answers0