Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Portability of Misaligned Data Access . . . . . . .



> Yeah, but it's a *lot* uglier than that.  Remember the header!  What
> you really need is something like:
>
> union {
>   struct {
>     char pad[6];
>     char input[10];
>   } namadata;
>   struct {
>     union {
>       struct {
>         char pad[6];
>         char header[2];
>         } prefix;
>       double align;
>     }
>     double payload;
>   } yakidata;
> } do_you_really_expect_to_pull_this_off_interrobang;

Funny... I was going to write something like that but I thought better of
it. I was assuming he'd rather copy the interesting chars from the 17-byte
struct into the union manually than tackle the data structure necessary to
grab the payload from the original string on the fly.

> and even then there's no guarantee you'll get a packed structure, or
> even that sizeof(char) == 1.

Hmmm... my Stroustrup is is pretty old but it does say (sec. 5.3.2) that:

  "... a byte is undefined by the language except in terms of the value of
   sizeof: sizeof(char) is 1."

Of course, if that's no longer the case, you're always free to use:

   sizeof( double ) / sizeof( char )

assuming it divides evenly (oh... the pain ;-)...

I've seen much worse. A huge program saving data in a non-standard binary
format, written before 32-bit machines even existed, later ported by some
crazed lunatic to run on a 32-bit machine. Offsets computed by taking a
pointer to a representative instance of a number (usually zero) and doing
a combination of casts, shifts, and subtractions. I should have saved some
examples, as they would make excellent exam material for a course in C ;-).

> The former looks like an exceptionally nasty and pointless form of
> data torture.

Yeah... if they're going to the trouble of emitting an apparently ASCII
header, would that not suggest that the whole message is in ASCII?

Maybe it would be possible to get a hex dump of the actual bytes returned
from this meter -- or to get the model number so someone can try to google
up a programmer's manual.

---
Joseph L (Joe) Larabell            Never fight with a dragon
http://larabell.org                     for thou art crunchy
                                  and goest well with cheese.


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links