Hey everyone, I have yet another question on allocated objects on the heap. Lets say I have an object and I allocate memory for it:
Object *object = new Object;
If I allocated the object on the heap and want to delete it in the destructor like:
1 2 3 4
Object::~Object()
{
delete object;
}
From what I've heard is that the destructor will just be called and ignore the delete leaving the object to not return to the heap, or maybe I'm mistaken and this works. I also heard if you delete it before the destructor the destructor will be called anyways and ignore the delete so how does someone actually delete an object they allocated on the heap if the destructor gets in the way.
also if I make an object from a struct like:
1 2 3 4
typedefstruct Object
{
/*stuff goes here*/
}
-in main- Object *object = new Object;
would I need a constructor or can I do this and not need a constructor and just use it to allocate memory for this object.
Please if anyone can help that be great, I really want to understand how this works because with malloc() and free() no constructor is involved.
If you are creating object inside main and writing delete object inside class, it will simply give you compilation error as object is not known to the class in the destructor as compiler goes from top to bottom.
you can do like this:
lass A
{
int i;
public:
A()
{
i= 0;
cout<<"Constructor" << endl;
}
~A()
{
cout << "destructor" << endl;
}
};
int main()
{
A *obj = new A;
delete obj;
return 0;
}
So here while creating object constuctor will be called and while deleting destructor will be called.
You call the destructor with 'delete' and after that the memory will be cleaned. You can't call 'delete' in the destructor of your object because delete needs a pointer but your class is only a template for your object! Your class doesn't know your class instance 'object'.
If you allocate the object with new you delete the object with delete.
1 2
Object *object = new Object; // this will call the default constructor
delete object; // this will call the destructor
If you instead create the object the 'normal' way the object will be deleted when it goes out of scope.
1 2 3 4 5
{
...
Object object; // this will call the default constructor
...
} // the destructor will be called here because object goes out of scope
The object itself should not have to know how it was allocated. It should not delete itself in the destructor. If the object allocates some other object by using new (perhaps in the constructor) the destructor is a good place to delete it.
I think you are confusing constructors/destructors with memory allocation here.
Constructors and destructors in themselves have nothing to do with memory allocation/freeing - but they can be used for it.
Constructors are member functions of a class that are called when an object of that class is created, similarly destructors are called when objects are destroyed.
So if you have member data in your class that needs dynamic allocation, you would do so in your constructor, so that the memory is allocated and ready for the object to use.
In that case you have to de-allocate that memory at some point, preferably when it is not needed any more. This is most likely the moment that the object itself is destroyed, so in the destructor.
Also - I'd like to get one the experts views on whether objects are created on the heap. For as far as I know, as long as you don't declare the objects globally, they are put on the stack.
Thanks everyone for the help, I didn't really understand but now I got some idea. What if though, I wanted to pass the object around after it gets allocated to another function to work with it there for the moment and then bring it back to where it came from so:
-in main-
1 2 3
Object *object = new Object;
someFunction(object);
-another file with someFunction-
1 2 3 4 5 6 7
someFunction(Object *object1)
{
// do stuff with object1
// so would I need to do something like this?
return object1;
}
Do I need to return the object back and store it again? How does it work if you pass memory allocated objects to functions? Do you delete both, one in the function and the other in main?
No, you don't need to perform any deletes in the function.
You're passing a pointer to the object to the function. The function will be accessing the same object that you've allocated space for in main.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// In main
Object *object = new Object;
// Print the address the pointer is pointing to
cout << "Pointer is pointing to address: " << object << endl;
SomeFunction(object);
// someFunction
void SomeFunction(Object* obj)
{
// Print the address the pointer is pointing to
// It'll be the same as the one in main, hence the same object
cout << "Pointer is pointing to address: " << obj << endl;
obj->DoStuff(); // Is the same as doing object->DoStuff() in main
}