Go Down

Topic: Class code added twice to compiler output (Read 377 times) previous topic - next topic

juliangall

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
Code: [Select]
class MyClass {
 private:
   int _val;
 public:
   MyClass (int val);
};

MyClass.cpp
Code: [Select]
#include "MyClass.h"

MyClass::MyClass (int val) {
 _val = val;
}


Look at the output from avr-objdump (thanks mem!) created using:
Code: [Select]
avr-objdump -S -t Test.elf > temp.txt

It has:
Code: [Select]
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

follower

Yeah, as I recall it's a known "issue" with avr-gcc. From memory it's something related to inheritance or something. I seem to recall it also causes issues with debugging...

Anyway, if you search you should find much more discussion on it than you would ever want. :-)

--Phil.

Go Up