Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I declare a destructor in SystemC?

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?

like image 322
Marco Avatar asked Jun 24 '16 17:06

Marco


People also ask

How do you declare a destructor?

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.

How do I add a destructor in C++?

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.

Which operator is used to declare destructor?

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.

Do I need to declare a destructor?

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.


1 Answers

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;
};
like image 117
Guillaume Avatar answered Sep 22 '22 13:09

Guillaume