I'm writing a module in SystemC where within the constructor I have a variable initialized with new
:
SC_CTOR(MY_MODULE)
{
...
...
my_matrix = new unsigned char [a*b];
...
...
}
How can I declare the destructor to release the memory when the simulation ends?
A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() . If you do not define a destructor, the compiler will provide a default one; for many classes this is sufficient.
A destructor is a member function with the same name as its class prefixed by a ~ (tilde). For example: class X { public: // Constructor for class X X(); // Destructor for class X ~X(); }; A destructor takes no arguments and has no return type.
Like a constructor, Destructor is also a member function of a class that has the same name as the class name preceded by a tilde(~) operator. It helps to deallocate the memory of an object.
No. You never need to explicitly call a destructor (except with placement new ). A class's destructor (whether or not you explicitly define one) automagically invokes the destructors for member objects. They are destroyed in the reverse order they appear within the declaration for the class.
You need to use the C++ semantic. There is no equivalent of SC_CTOR
for the destructor.
SC_MODULE(MyModule)
{
SC_CTOR(MyModule)
{
my_matrix = new unsigned char [10];
}
~MyModule() {
delete my_matrix;
}
private:
unsigned char * my_matrix;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With