0

I'm really having a hard time understanding pointers, so please forgive me for any ambiguity this question may have. Yes, I'm asking more than one question, the one in the title is by far the most important, though. The other questions are just questions that I would really appreciate you answering. So lets say I have a class which looks something like this:

class myclass
{
public:
    myclass();
    ~myclass();
private:
    struct node
    {
        node * next;
    }
    node * head;
    int myint;
}

Now, as I'm aware, one would call constructors for the class and struct, as well as deconstructors for each. I've been told that integers shouldn't be deleted in the deconstructor, but what about deleting the pointer to the integer. Shouldn't that be deleted in the deconstructor like this:

myclass::~myclass
{
    delete head;
    delete &myint;
}

If not, why?

If you want to just answer that question, read no further. For the sake of brevity, my next question is about this code, and the deconstructor above, which I've been told is wrong (assume this is part of the same file as the deconstructor above):

myclass::node::~node
{
    delete next;
}

More specifically, I've been told I shouldn't delete nodes unless they've been declared with a new tag. Needless to say, I find that very confusing. If you can, could you explain what they mean by that and why what they're saying is true? A link explaining that would be equally appreciated, just understand that I'm new to coding, I just started learning C++ a few months ago.

Larrimus
  • 211
  • 1
  • 2
  • 9
  • 1
    The rule is simple: if you allocate it, you need to deallocate it eventually. – Barmar Sep 26 '15 at 00:17
  • 1
    http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome?lq=1 – Barmar Sep 26 '15 at 00:18
  • 1
    @Barmar That's the only thing that's improved my understanding of pointers in a long time. Thank you! – Larrimus Sep 26 '15 at 00:32

2 Answers2

1

No, you should use delete only on pointers to memory that has been allocated via new, as per §5.3.5/1 of the C++ standard:

The delete-expression operator destroys a most derived object (1.8) or array created by a new-expression.


In fact, with regards to writing good C++ code, you shouldn't even delete that pointer yourself, and just use an std::unique_ptr and let the default destructor handle that.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 1
    Okay, but why is that the case? – Larrimus Sep 26 '15 at 00:18
  • Because that's the only thing `delete` can do, is free the memory that was allocated using `new`. – Barmar Sep 26 '15 at 00:19
  • @Larrimus Otherwise the memory will be deleted twice, once when you call delete on it, and once when the variable goes out of scope. That is bad!!! – Robin Hartland Sep 26 '15 at 00:20
  • @Larrimus It's like trying to return something you bought to a different store than you bought it. They won't give you your money back. – Barmar Sep 26 '15 at 00:20
  • @Larrimus Mainly because the C++ standard says so. – Shoe Sep 26 '15 at 00:22
  • Basically when you `new` something, you assume responsibility for the corresponding `delete` call, which should be done as soon as you can. Think of it as cleaning up after yourself. What `new` does is allocate memory on the heap (google it maybe), and that is what you need to kinda sorta undo later on. –  Sep 26 '15 at 00:22
  • @Barmar So there's no point to the deconstructor because I should usually just delete nodes declared with the `new` operator in the individual functions they're initialized in? – Larrimus Sep 26 '15 at 00:24
  • 1
    How is `std::unique_ptr` different from declaring a pointer with the `*` operator? – Larrimus Sep 26 '15 at 00:47
  • @Larrimus [Here](http://en.cppreference.com/w/cpp/memory/unique_ptr) – Shoe Sep 26 '15 at 00:54
1

You can only use delete on memory that was allocated dynamically using new.

In your case, myint is not a separate block of dynamically allocated memory. It's a member of the myclass object, and its memory cannot be managed separately from the containing object. When you allocate a myclass object (either as an ordinary variable, or dynamically using new myclass), all its members will be allocated; when you free it, all its memory will be reclaimed. You don't need to delete the individual members.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    I've never had to call the deconstructor, though. Will I have to call a class's deconstructer in future, more complex programs? – Larrimus Sep 26 '15 at 00:28
  • You don't call the deconstructor directly, it's done automatically for you. Either when you delete the pointer to the object, or when the variable containing the object goes out of scope. – Barmar Sep 26 '15 at 00:37
  • 1
    But the object (or other memory) that the pointer points to is not released when the pointer goes out of scope. – Larrimus Sep 26 '15 at 00:41
  • 1
    That's correct. That's why you have to use `delete ptr;` before the pointer goes out of scope. There are also "smart pointer" classes that automate much of this for you. – Barmar Sep 26 '15 at 00:43