0

I have made a quite large program (not executed at an Arduino lately). However, if I compile with the next fragment:

void LightSetupClass::CreateFixtures()
{
    for (fixture_number_t fixtureNumber = 0; fixtureNumber < NR_OF_CHINESE_PARS; fixtureNumber++)
    {
        _pars[fixtureNumber] = new ChinesePar();
    }

The memory usage is:

Sketch uses 11558 bytes (35%) of program storage space. Maximum is 32256 bytes. Global variables use 1208 bytes (58%) of dynamic memory, leaving 840 bytes for local variables. Maximum is 2048 bytes.

When I commented out the new allocation:

void LightSetupClass::CreateFixtures()
{
    for (fixture_number_t fixtureNumber = 0; fixtureNumber < NR_OF_CHINESE_PARS; fixtureNumber++)
    {
        //_pars[fixtureNumber] = new ChinesePar();
    }

The compiler writes:

Sketch uses 10306 bytes (31%) of program storage space. Maximum is 32256 bytes. Global variables use 1163 bytes (56%) of dynamic memory, leaving 885 bytes for local variables. Maximum is 2048 bytes.

The big difference in the sketch size is probably related to the fact that this is the only location where the ChinesePar instances are created, resulting in that complete class to be compiled/linked along.

However, what I don't understand, is that the SRAM memory changes ... My assumption was that a new dynamic memory allocation occupies space in the heap, which is not known at compile time, thus SRAM memory should not be affected.

(Btw, the local variable fixture_number_t can maybe make a difference but is only 1 byte). NR_OF_CHINESE_PARS is a constant set to 14.

0

I think I found the answer (and I post it in case others have a similar problem, I also update the question title to be able to find it easier):

The class is an overridden class and has several virtual overridden functions. For the class itself (since the compiler knows it is used), a v-table is created for those virtual functions. This happens only once, not per instance (which is why the compiler can check it before run time).

(too bad the actual size for the instances is put in heap space, meaning my Arduino Uno is not big enough to handle the storage).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.