Looking at the compiler output of a sketch, I noticed that some class members seem to have been compiled twice. i.e. Exactly the same code is generated at two different places, taking up twice the memory needed. I first became aware of this when moving code from a sketch to a class file - it increased the code size, which seemed odd. I have reproduced this in a very simple example.
Make a copy of the Blink sketch and add two files:
MyClass.h
class MyClass {
private:
int _val;
public:
MyClass (int val);
};
MyClass.cpp
#include "MyClass.h"
MyClass::MyClass (int val) {
_val = val;
}
Look at the output from avr-objdump (thanks mem!) created using:
avr-objdump -S -t Test.elf > temp.txt
It has:
0000013a <_ZN7MyClassC2Ei>:
#include "MyClass.h"
#include "WConstants.h"
13a: fc 01 movw r30, r24
MyClass::MyClass (uint8_t val) {
13c: 71 83 std Z+1, r23 ; 0x01
13e: 60 83 st Z, r22
_val = val;
140: 08 95 ret
00000142 <_ZN7MyClassC1Ei>:
#include "MyClass.h"
#include "WConstants.h"
142: fc 01 movw r30, r24
MyClass::MyClass (uint8_t val) {
144: 71 83 std Z+1, r23 ; 0x01
146: 60 83 st Z, r22
_val = val;
148: 08 95 ret
In other words, the constructor is being compiled once at location 13a and again at location 142.
Can anyone explain what is happening here?
Thanks,
Julian