-1

I am relatively new to C++ and trying to figure out how to properly delete structures. I understand that the delete operator should only be used on pointers created using the new operator.

However, in the context of structures and particularly when used within a binary tree, I have now often seen something like:

struct test_structure {
    int test_content;
}; 
test_structure *test_realization;
// Some code  
delete test_realization;

I do not quite understand why this is okay, even though no new operator was used to create test_realization. Or am I missing something here?

x3t2h
  • 151
  • 5
  • The compile-time checking required to ensure the programmer hasn't smurfed up would have to be extensive and border on insane. I don't think it would be possible in a single pass compiler. However in many cases the compiler will be able to emit a warning about an uninitialized variable being used. – user4581301 Jun 30 '18 at 00:25
  • thanks, so it is simply wrong. Could you just confirm for me please whether the use of delete in the code of this question (https://stackoverflow.com/questions/20135419/pointer-to-struct-and-delete) is also wrong? I was just a bit confused as nobody pointed that out ... – x3t2h Jun 30 '18 at 00:28
  • 1
    The code in the post you linked does not call `delete` on pointers that were not allocated by call to `new`. I am not sure why you think otherwise. – R Sahu Jun 30 '18 at 00:33
  • 2
    Side note: The best way to avoid having to deal with `new` and `delete` is to not use them. Use Automatic allocation (`test_structure test_realization;`) where possible. Use Standard Library containers like `std::vector` when you need large or in-determinate numbers of objects. When you can't avoid pointers, [use smart pointers](https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one). – user4581301 Jun 30 '18 at 00:42
  • If you find yourself somehow trapped into `new` and `delete` make absolutely certain you've nailed down Ownership (who is responsible for care and feeding and ultimately calling `delete`) of the allocation. Also read [What is meant by Resource Acquisition is Initialization (RAII)?](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) and [What is the Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – user4581301 Jun 30 '18 at 00:45
  • @ R Sahu sorry, I posted the wrong link in my previous comment. I meant to ask you about the accepted answer to that question https://stackoverflow.com/questions/22121257/how-do-i-properly-delete-nodes-of-linked-list-in-c – x3t2h Jun 30 '18 at 00:48
  • @ user4581301 yeah I would prefer to use autoamtic allocation, but I am afraid that this is not possible for my application. I am working on a branching tree and at some point I have to prune, i.e. delete, nodes from the tree as I will run out of memory otherwise. Hence, I cannot wait for my objects to be out of scope. Also, would there even be any other way to delete nodes from a binary tree without using new and delete? – x3t2h Jun 30 '18 at 00:53
  • Are you sure `//some code` doesn't include something that assigns `test_realization` with a pointer returned by `new`? – Barmar Jun 30 '18 at 00:55
  • The problem being pointed out in that answer is with `Node *current = new Node;` the Asker allocated a `Node` they never used and promptly lost at `current = front->next` a few lines later. Never `new` until you're forced to. Do not allocate in case you might need to; allocate right before you need to. `delete` as soon as you don't need the memory any more in case you forget. – user4581301 Jun 30 '18 at 00:55
  • "any other way to delete nodes from a binary tree without using new and delete?" This will take a while to get through, but Herb Sutter's got a few suggestions for you on that: https://www.youtube.com/watch?v=JfmTagWcqoE He breaks down a binary tree and explains how to do it with smart pointers. – user4581301 Jun 30 '18 at 00:57
  • I just noticed WhozCraig's little addition at the bottom of the answer. Pay close attention to the double pointer trick. That sucker can save you tonnes of coding debugging time when you're working on pointer based structures like trees and lists. Also note how `pp` is never touched by `new` or `delete`. It never owns a pointer. Someone else does and is responsible for deletion. – user4581301 Jun 30 '18 at 01:06

1 Answers1

1

Looks like you are confused by term "created using the new operator". So when you write:

 test_structure *test_realization = new test_structure;

you do not create test_realization itself using operator new, you create an object, pointer to it returned and assigned to test_realization. Such object can be destroyed later by operator delete. test_realization is a variable which has type pointer to test_structure and like any other variable it can hold different values, can be initialized when defined, may not. So when somebody says pointer "created using the new operator" he means value, that you assign to a test_realization not variable test_realization itself.

 test_structure *test_realization;
 ...
 test_realization = new test_structure; // now test_realization points to object that created by new
 test_realization->test_content = 123; // we can use that object
 ...
 delete test_realization; // now object, which was created by new destroyed and memory released

Though it is a good idea to define and always initialize variable, it is not required.

Slava
  • 43,454
  • 1
  • 47
  • 90