Pages: [1]   Go Down
Author Topic: !#%$ casts  (Read 882 times)
0 Members and 1 Guest are viewing this topic.
Minnesota USA
Offline Offline
Sr. Member
****
Karma: 1
Posts: 323
Made it mahself outta sand 'n wahr.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 210
Posts: 13036
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Minnesota USA
Offline Offline
Sr. Member
****
Karma: 1
Posts: 323
Made it mahself outta sand 'n wahr.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks. That looks much better!
Logged

Pages: [1]   Go Up
Jump to: