Pages: [1]   Go Down
Author Topic: Class code added twice to compiler output  (Read 353 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
class MyClass {
  private:
    int _val;
  public:
    MyClass (int val);
};
MyClass.cpp
Code:
#include "MyClass.h"

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

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

It has:
Code:
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
Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: