Reputation: 399
I am new to C++. From what I have read, unlike java, C++ does not have automatic garbage handling, so in classes we add destructors for objects:
class A{
class A();
~class A(); // destructor
};
I am curious on what happens when we use structs. Also, what happens when we do not add destructors to the class? How is the memory deallocated?
Upvotes: 1
Views: 1978
Reputation: 913
I think a good way to answer is to compare Java to C++. In Java, all reference types are stored in the heap, while all primitives are stored in the stack. What this means is, primitives are destroyed when they go out of scope; that is, whenever the method in which they are declared returns. On the other hand, this is not the case with reference types.
In C++, you control which variable is stored dynamically (in the heap) or automatically (in the stack). If you create an object with the new
operator, it remains in the heap until you free that memory with a corresponding delete
. If you create a variable (or object) without the new
operator, its memory is freed when the object goes out of scope.
To answer your question about contructors and destructors: a contructor is a method that is always called upon the creation of a new object. If you do not specify any constructor, a default one (which takes no arguments) is implied. The destructor is called when the object goes out of scope, or if you delete it with the delete
operator (in the case that you created it dynamically with new
).
Upvotes: 1
Reputation: 76240
C++ doesn't have any garbage collection. That's almost true. Destructors are a very useful tool that are also used to collect the garbage.
In a class there are 4 predefined methods: the default constructor, the copy constructor, the overloading of the assignment operator and the destructor. This means that this methods have a default version:
When you are defining:
class A {
private:
int* a;
public:
A() { a = new int(5); }
};
the default destructor will just erase the memory allocated by the pointer in itself, without deallocating the memory that the int(5)
took.
Therefore we need to define a specific destructor:
~A() { delete a; }
or even better use a smart pointer.
Upvotes: 1
Reputation: 5064
The same rules that apply to classes also applies to structs in C++, with the exception being the default access level, and no other.
If you do not define a destructor, the compiler defines a default destructor for you. Both for classes and structs.
The default constructor calls base class'es destructor and destructors of all members which have them. The default destructors are just a commodity in case you do not need anything special done with your class you do not need to write an empty version manually (Like deallocating a dynamically allocated object from the heap, or managing non-automatic members).
Upvotes: 1
Reputation: 9691
The only difference between a struct and class in C++ is default access control--members are private by default in classes and public in structs. If you don't define your own destructor the compiler gives you a default destructor; when the object is destroyed all of the members are too. You only need to define your own destructor if any of the class members point to heap allocated memory.
Upvotes: 1