-1

Is it possible to use delete operator on class instance without new operator to deallocate memory? Like this:

Class myclass{public void hi{std::cout<<"hi\n";};}

myclass class1;
delete &class1;
class1.hi(); //results in error

Thanks

Nick
  • 106
  • 2
  • 10
  • 3
    possible: yes, correct: no way. For every `new` there should be one `delete` and vice versa – 463035818_is_not_an_ai Feb 06 '18 at 13:40
  • 2
    Why do you think you need to do it? – StoryTeller - Unslander Monica Feb 06 '18 at 13:41
  • I was just curious :D – Nick Feb 06 '18 at 13:42
  • I'm pretty sure this is explicitly stated in the standard of the language and in every C++ tutorial. Now you cannot do that. For most implementations that memory resides on the stack and cannot be freed nor would you be able to use it in a meaningful way. Read up on the stack and the heap (eg [here](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) for a better understanding of the underlying mechanics, but keep in mind this is implementation detail and not the C++ language. – patatahooligan Feb 06 '18 at 13:42
  • A slightly more interesting question is if you can call `delete` on a pointer that was previously allocated with `malloc` (not `new`) – Gillespie Feb 06 '18 at 13:44
  • @RPGillespie - It's exactly the same answer though – StoryTeller - Unslander Monica Feb 06 '18 at 13:45
  • 1
    @RPGillespie: For me the only interesting question is what happens if you have something like `delete new foo[1];` – Bathsheba Feb 06 '18 at 13:46

1 Answers1

3

No, the behaviour would be undefined.

You should only use delete on a pointer that's been given to you by new. The type of the pointer needs to be the same too, unless it points to an appropriate instance of a polymorphic class.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Then why does the program results in error on function call after delete operator and not on delete operator? – Nick Feb 06 '18 at 13:41
  • 2
    @Nick: That's one manifestation of undefined behaviour. Informally, you've probably completely rogered your stack. – Bathsheba Feb 06 '18 at 13:41
  • Doesn't it mean that delete actually deletes the nonpointer type object – Nick Feb 06 '18 at 13:43
  • 3
    @Nick: Maybe, maybe not. I don't think you're grasping what undefined behaviour means here. – Bathsheba Feb 06 '18 at 13:43
  • No, undefined behavior means it does anything at all. It can vary across implementations, compiler versions and even different programs that use some common section of code. It might happen to do what you expected it to do but the behavior is absolutely volatile and cannot be relied on. – patatahooligan Feb 06 '18 at 13:44
  • @Nick undefined means that it will vary from compiler to compiler. You might get one result with Visual Studio, and another result with GNU – Gillespie Feb 06 '18 at 13:45
  • @RPGillespie That's implementation-specific behavior. – DeiDei Feb 06 '18 at 13:45
  • I understand now what you mean – Nick Feb 06 '18 at 13:46
  • @Nick: Basically it means that the compiler could eat your cat. And I can't resist plugging the question I answered yesterday: https://stackoverflow.com/questions/48623709/if-a-part-of-the-program-exhibits-undefined-behavior-would-it-affect-the-remain/48623785#48623785 – Bathsheba Feb 06 '18 at 13:46
  • @DeiDei I know, but I was giving a layman's example so it's easier for Op to understand. A lot of undefined behavior (according to the spec) is "defined" in specific implementations – Gillespie Feb 06 '18 at 13:47
  • Reference for the various types of behaviour: http://en.cppreference.com/w/cpp/language/ub – Richard Critten Feb 06 '18 at 13:48
  • Thanks guys, i will definitely check out more info on undefined behavior – Nick Feb 06 '18 at 13:50