Duplicated constructors in object file

Hi everybody,

I'm new to arduino and the forum and this is my first post. I hope I'm posting it in the right forum...

I started with my first project and my binary excess the maximum size to fit in the atmega168, so I had a look at the object files in the /applet folder of my sketch with avr-objdump and found something I think is weird. The constructors of every class are duplicated, they are created twice.

For example, if I do "avr-objdump -CD my_sketch.elf" I can see this for the HardwareSerial class:

000022c0 <HardwareSerial::HardwareSerial(unsigned char)>:
    22c0:       08 95           ret

000022c2 <HardwareSerial::HardwareSerial(unsigned char)>:
    22c2:       08 95           ret

000022c4 <HardwareSerial::printNumber(unsigned long, unsigned char)>:
    22c4:       cb 01           movw    r24, r22
    22c6:       ba 01           movw    r22, r20
    22c8:       33 27           eor     r19, r19
    22ca:       44 27           eor     r20, r20
    22cc:       55 27           eor     r21, r21
    22ce:       0e 94 e6 12     call    0x25cc  ; 0x25cc <printIntegerInBase>
    22d2:       08 95           ret
...

You can see that the HardwareSerial constructor is duplicated, and I think this is wasting space, not much in this case, but if I look for one class with a more complex contructor this is worse:

0000010a <LCD4Bit::LCD4Bit(int)>:
     10a:       70 93 0f 01     sts     0x010F, r23
     10e:       60 93 0e 01     sts     0x010E, r22
     112:       61 50           subi    r22, 0x01       ; 1
     114:       70 40           sbci    r23, 0x00       ; 0
     116:       62 30           cpi     r22, 0x02       ; 2
     118:       71 05           cpc     r23, r1
     11a:       30 f0           brcs    .+12            ; 0x128 <LCD4Bit::LCD4Bit(int)+0x1e>
     11c:       81 e0           ldi     r24, 0x01       ; 1
     11e:       90 e0           ldi     r25, 0x00       ; 0
     120:       90 93 0f 01     sts     0x010F, r25
     124:       80 93 0e 01     sts     0x010E, r24
     128:       08 95           ret

0000012a <LCD4Bit::LCD4Bit(int)>:
     12a:       70 93 0f 01     sts     0x010F, r23
     12e:       60 93 0e 01     sts     0x010E, r22
     132:       61 50           subi    r22, 0x01       ; 1
     134:       70 40           sbci    r23, 0x00       ; 0
     136:       62 30           cpi     r22, 0x02       ; 2
     138:       71 05           cpc     r23, r1
     13a:       30 f0           brcs    .+12            ; 0x148 <LCD4Bit::LCD4Bit(int)+0x1e>
     13c:       81 e0           ldi     r24, 0x01       ; 1
     13e:       90 e0           ldi     r25, 0x00       ; 0
     140:       90 93 0f 01     sts     0x010F, r25
     144:       80 93 0e 01     sts     0x010E, r24
     148:       08 95           ret

0000014a <LCD4Bit::pulseEnablePin()>:
     14a:       1f 93           push    r17
     14c:       61 e0           ldi     r22, 0x01       ; 1
     14e:       80 91 04 01     lds     r24, 0x0104
     152:       0e 94 52 15     call    0x2aa4  ; 0x2aa4 <digitalWrite>
     156:       15 e0           ldi     r17, 0x05       ; 5

I think the constructors should appear only once and this is wasting some of the limited space in the atmega168.

Does anyone know why this is happening? Is it normal?

hey thanks for th report
we'll have a look at it... if it does it in every program then it could be a bug in the compiler...

massimo

I have tried with some of the examples that comes with arduino and I the result is the same, so I think the problem is not related to my sketch.

Can someone test it on his/her system to check if the problem is related to my compiler/system or is a general problem?

Thanks.

Same on my system (arduino-0010 on XP). Two hardware serial constructors in the asciitable communication example sketch.

BTW, is there an easy way to run objdump on the current sketch from the IDE?

I don't know how to run objdump from the IDE.

I forgot to mention I'm using arduino-0010 on a Debian GNU/Linux system and avr-gcc v4.2.3

I found a bug already logged for gcc 3187 – gcc lays down two copies of constructors. It looks like a quite old issue with a long discussion. Although this issue may not be very important in some situations I think is pretty important for embedded systems.

I read on the discussion that there was a patch to solve this that was never approved, I'll have a look at this to see if it can be helpful to reduce the size of arduino sketchs.