Go Down

Topic: !#%$ casts (Read 915 times) previous topic - next topic

jrraines

Please take a look at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1278172720
I've been struggling (mostly brute force trial and error) to fix some code in eeprom.h, deep in the bowels of arduino. The code is conditionally included if the processor is a 2560. So although it is in the current system source it doesn't get compiled. My hunch is that it works on another compiler, but not gcc.

Perhaps part of what leaves me so confused about trying to understand casts is that the rules seem to be interpreted differently in different compilers and I find varying explanations of C++ casts on the web. Anyway the one case I worked out a sort of solution for seems awkward and wordy and therefore seems inappropriate for a fundamental part of the system. There must be some way to get the compiler to agree to "copy anything", which is what the original code seems quite clearly to tell it to do.

Unfortunately I think the problem is way more pervasive than the 2 instances I posted, but it seems to me that if I can write the syntax elegantly once, I will understand how to do it over and over.

Coding Badly

Quote
My hunch is that it works on another compiler, but not gcc.

The problem lies not with the compiler but with the fact that whoever wrote that code only tested it with C-compilation1.  Had they run the code through ANY C++ compiler they would be having the same problems you're having.

This compiles and should work correctly...
Code: [Select]
static __inline__ void
eeprom_write_block (const void *__src, void *__dst, size_t __n)
{
#if (! (defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)) )
 __eewr_block (__dst, __src, __n, eeprom_write_byte);
#else
 /* If ATmega256x device, do not call function. */
 while (__n--)
 {
   eeprom_write_byte( (uint8_t*)__dst, *(uint8_t *)__src );
   __src = (uint8_t*)__src + 1;
   __dst = (uint8_t*)__dst + 1;
 }
#endif
}


Don't worry about optimization.  Writing to EEPROM is a very slow operation.  Relatively speaking, a few extra machine instructions is going to make no difference.


1 The code may not even be compilable C.  I'm too lazy to test and too ignorant to know for certain.

jrraines

Thanks. That looks much better!

Go Up