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


Relevant Pages

  • Re: problem with sizeof
    ... WR> from the above structures is 3 characters and 5 characters ... but it would not be at all unusual for padding to be ... WR> Probably what you will have to end up doing is using a union ... unsigned char function; ...
    (comp.lang.c)
  • Re: problem with sizeof
    ... WR> from the above structures is 3 characters and 5 characters ... but it would not be at all unusual for padding to be ... WR> Probably what you will have to end up doing is using a union ... unsigned char function; ...
    (comp.lang.c)
  • Re: Syntax for union parameter
    ... unsigned char blu; ... The standard allows an implementation to insert arbitrary padding ... between members of a struct. ... Certainly older ARM processors ...
    (comp.lang.c)
  • Re: Memory initialization
    ... >> I myself wouldn't pay a blind bit of notice to changing padding bits. ... padding bits can be written as in the case of POD types: ... of the array of char or unsigned char is copied back into the object, ... T obj; // obj initialized to its original value ...
    (comp.lang.cpp)
  • Re: Meaning of memset (Was: Re: Assigning values to char arrays)
    ... if unsigned char has padding bits in its representation (which I ... No. unsigned char may not have padding bits. ... the object representation where all the ... bits are zero shall be a representation of the value zero in that ...
    (comp.lang.c)