I work on a project that is using C++ code in a PIC32 microcontroller. C++ classes are used to structure the pages and widgets used on display.
All pages extends the base class Page
. All pages contains widgets. All widgets extends the base class Widget
. Both of the base classes have some virtual functions.
Here is an heavily simplified example of the code just to show the global structure :
class PageHome : public Page
{
public:
//...
virtual void paint(void);
virtual void repaint(void);
private:
//...
}
class PageHome : public Page
{
public:
//...
void paint(void);
void repaint(void);
private:
WidgetLabel *m_lblTest1;
WidgetLabel *m_lblTest2;
}
class PageConfiguration : public Page
{
public:
//...
void paint(void);
void repaint(void);
private:
WidgetLabel *m_lblTest1;
WidgetLabel *m_lblTest2;
}
void main()
{
Page *pPageHome = new PageHome();
Page *pPageConfiguration = new PageConfiguration();
//...
}
Now, what I observe is a big difference in program size after compilation when using widgets or not in pages.
Second page not using WidgetLabel :
Program Used: 245,332 (0x3BE54) Free: 16,812 (0x41AC)
Second page not using only one WidgetLabel :
Program Used: 250,532 (0x3D2A4) Free: 11,612 (0x2D5C)
Second page not using two WidgetLabel :
Program Used: 250,628 (0x3D304) Free: 11,516 (0x2CFC)
Each time I use a widget for the first time in one page (already used in other pages), the first instance consume a lot of memory, arround 5KB of program memory. Other instances consume less. Maybe I’m wrong, but I was expecting this 5KB memory usage only for the first instance for the complete program, not per page. So, is it normal? Could it be because of a bad structure design?
The code is compiled for PIC32 using MicroChip xc32 v2.50. Tested with other more recent versions without significant differences (not counting bytes but the memory behavior).