How to deallocate memory from an instace of class

Hello. First, sorry for my bad English. I've read several times that it is possible to "delete" an instance of a class using delete expression(or it is automatically deleted when out of scope), and i'd like to know, does the code below "delete" an instance of class? Does it deallocate memory from the address?

class header file:
1
2
3
4
5
6
7
8
#pragma once
class TEST
{
public:
	TEST();
	~TEST();
	void Test();
};


class source file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include "TEST.h"
#include <iostream>


TEST::TEST()
{
}


TEST::~TEST()
{
}

void TEST::Test()
{
	std::cout << "I'm alive\n";
}


main source file:
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include <iostream>
#include "TEST.h"

int main()
{
	TEST River1;
	delete &River1;
	//River1.Test();

	system("pause");
}

Last edited on
you don't.

your class is automatically deallocated when the scope for its instance ends, here, River1 goes out of scope at the end of main, and it is destroyed there. There are ZERO pointers in this code to deallocate.

This simple rule can be hard to follow in large programs, but ... you need 1 delete statement for every new statement. You have 0 new statements. You need 0 delete statements. You can write code that breaks this rule and is still correct, but its not advisable in most scenarios.


You cannot delete a reference like this, it is not correct. Just remove line 8 entirely, and its fine. You also should not call destructors .. it is done for you. You sometimes have to write them, but you don't actually CALL them.

You don't need a destructor for this class. Nor a constructor.


if you need to explicitly delete an instance of a class, you either need a pointer to the class with memory from a new statement OR you need to have stored the instance in a standard container that allows deletion. For example, you can erase 1 instance from a vector or remove one from a list, etc.

But a free floating class instance that is just a variable, you cannot delete that any more than you can delete an integer variable in the middle of main. User defined types work a lot like built in types (int, double, etc) ...
Last edited on
@jonnin thanks for your reply! well, visual studio created constructor and destructor for me, i didn't. I know about new statements, but when i compile and run the code above, with "//" removed, the program crashes, that's what confused me. the program crashes at line 9, not at line 8, I would be thankful, if you could explain this to me.

well, if line 8 actually worked, line 9 would be tapping the destroyed class ...

Ive been known to be wrong. May be 8 is legal... Ill see if I can find out in a bit.

The very few times I have wanted to do something like this I just put bogus {} around it to scope it.

I tried this same code on g++ and line 8 compiled, and it called the destructor, and then it crashed even if the destroyed class was not accessed afterwards. The act of deleting the class caused an instability. I do not know why (unless I was right the first time, no-can-do). I added a print to my destructor, and the line 8 clearly calls the dtor.

I will revisit this later. Ive been up 36 hours and am losing focus.
Last edited on
I knew delete operator was for new statements, but I was wandering and tried it on class, if it was only for dynamic memory, then it wouldn't destroy the class and give an error, but it didn't, instead, seems that it destroyed the class, so i wrote it here. Please let me know if you find anything about this.
delete is dumb, it will take a crack at any pointer, no matter how you got the memory. I know that, and I know it blows up if the memory isnt yours to delete. Past that... unsure.
Topic archived. No new replies allowed.