Subj : Re: Any help on why? To : borland.public.cpp.borlandcpp From : Bob Gonder Date : Thu Jul 08 2004 04:09 pm Thomas Maeder [TeamB] wrote: >Bob Gonder writes: > >>> In contrary to your code, mine is at least correct. >> >> Not quite, yours is also wrong-endian on Win/Intel. > >Mine is endianness agnostic. Don't see how that can be. >>>>>unsigned char const *const begin(reinterpret_cast(&d)); This returns the lowest address (the LSB) of d. (MSB in BigEndian, right?) >>>>>unsigned char const *const end(begin + sizeof d); This returns the address 1 past d (1 past MSB) (Again, LSB in BigEndian, right?) >>>>>for (unsigned char const *c = begin; c!=end; ++c) This outputs data LSB-to-MSB, which is backwards for a hex value. In BigEndian, it would output MSB-to-LSB, which is the normal way to display a hex value. For long d = 0x12345678 Your output for LittleEndian would be 78563412 For Big Endian it would be 12345678 Hardly agnostic (or correct) We weren't talking hex dumps here (for which your code is fine), we were talking %x replacements for displaying hex values. For that, your code is broken. >>>>>double d(3.11415); >>>>>unsigned char const *const begin(reinterpret_cast(&d)); >>>>>unsigned char const *const end(begin + sizeof d); >>>>>for (unsigned char const *c = begin; c!=end; ++c) >> >> Should be c=end-1, c>= begin; --c > >That would have undefined behavior when c is moved to begin-1. Could be, but I'd do it completely different anyway. probably closer to c=end;do{--c;use c;}while(c != begin) And, being a library subroutine, I'd switch it, along with all my file and screen io routines, when changing platforms. I have no expectations that my libraries will survive intact when platforms collide. But, I'm trying to improve my applications so they rely on library code instead of platform code. >> Go along with that, but maybe better would be to fire those inept >> programmers (or retrain them). > >After all these things you posted in this thread, I doubt that you are in >a position to ask somebody else to get retrained, I'm afraid. But then, I'm not the code snob that abhors "inappropriate" usage. _If_ I were, I wouldn't look to ISO to "fix" the language, I'd "fix" the programmers. The language was just fine, thankyouverymuch. I do try to look ahead to 64, perhaps 128 bit x86 platforms, but I don't see worrying about Motorola or other cross-platform issues. It just isn't important to me, nor I'd think to most non-academics. Maybe large enterprises worry, but then they worry about lots of things that don't affect normal people. .