C++ Virtual destructors ROM usage

63 views Asked by At

I try to understand why it takes so much code to create a base class with a virtual destructor. I am getting ready to write a project on MCU with a small ROM space so it bothers me a lot.

#include "stm32g0xx.h"
#include "stm32g0xx_nucleo.h"

class test
{
    uint32_t data;
public:
    test(): data(0)
    {

    }
    ~test()
    {

    }
    uint32_t getData() const
    {
        return data;
    }
    void setData(uint32_t d)
    {
        data = d;
    }


};

int main(void)
{
    test t;
    for(;;);
}
   text    data     bss     dec     hex filename
    936    1096    1096    3128     c38 Destructors.elf
#include "stm32g0xx.h"
#include "stm32g0xx_nucleo.h"

class test
{
    uint32_t data;
public:
    test(): data(0)
    {

    }
    virtual ~test()
    {

    }
    uint32_t getData() const
    {
        return data;
    }
    void setData(uint32_t d)
    {
        data = d;
    }


};

int main(void)
{
    test t;
    for(;;);
}
   text    data     bss     dec     hex filename
   3256    2144    1160    6560    19a0 Destructors.elf

Creating virtual destructor takes 2k of .text and 1k of .data memory... Why? Is it possible to avoid virtual destructor and still write safe OOP program?

0

There are 0 answers