Re: Is it legal to type cast to DWORD* ???

From: Phlip (phlip_cpp_at_yahoo.com)
Date: 07/10/04


Date: Sat, 10 Jul 2004 16:44:41 GMT


"__PPS__" <block111@mail.ru> wrote in message
news:723debfe.0407100832.5ba15da9@posting.google.com...
> Thanks guys for your replies - it helped alot - from now on I will
> avoid this problem.

"Avoid" it by only doing it inside one function. Keep the rest of your
program ignorant of low-level data issues.

However...

> class something{
> union {
> unsigned char as_raw_data[4096];
> struct {
> unsigned char code;
> unsigned char identifier;
> unsigned char length[2];
> unsigned char authenticator[16];
> unsigned char pdu[4076]; //this later contains list of
> attributes.
> }as_pdu;
> } data;
> public:
> ...
> ...
> };
>
>
> When preparing a packet to be sent sometimes the unsigned char
> authenticator[16]; field is set to random 16 bytes. I do it using
> mersenne twister pseudorandom like this (random is a static instance
> of a class)
> *(DWORD*)(&data.as_pdu.authenticator[0]) = random;
> *(DWORD*)(&data.as_pdu.authenticator[4]) = random;
> *(DWORD*)(&data.as_pdu.authenticator[8]) = random;
> *(DWORD*)(&data.as_pdu.authenticator[12]) = random;

You did not present a reason to index authenticator at byte addresses. So
you might ought to do this:

> I wouldn't probably have this problem with:
> authenticator_as_ints[0] = random;
> authenticator_as_ints[1] = random;
> authenticator_as_ints[2] = random;
> authenticator_as_ints[3] = random;
>
> BUT, would my structure still be 4096 in total? (Looks like it should
> be for systems that I do test on - what about others??)

The padding between PODS data elements is implementation-defined. Plenty of
platforms provide #pragma pack() to prevent padding.

-- 
  Phlip
  http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces