problem with alignment of struct member datas (MSVC 4.2) 
Author Message
 problem with alignment of struct member datas (MSVC 4.2)

hi!

i've a little problem with alignment of structs includes
BYTE WORD and DWORD sized variables.
all of these variables are aligned on DWORD bounds, but in this case,
i can't mask (and read/write) a non-aligned void-pointed data structure.
HOW CAN I SWITCH OF VARIABLE-ALIGNMENT?? (ONLY IN SMALL CODE-PARTS!)
Shure, I could write this part in assemler, but i've to port the code,
and I dont like assembler for type casting.. (unreadable..)
In borlandc++ i know some switchs and castings, but they are all invalid
in msvc 4.0 .

please send a sample with your sollution to:

THANK YOU!!

-> struct Header
        {
        WORD VarWord;
        BYTE VarByte;
        DWORD VarDword;
        ... // 50 other variables with BYTE/WORD and DWOR sizes
        };

if i save raw, untyped data in this struct:
->      Header m_Header= *(Header*)VoidPointer;

only DWORD parts of raw data are stored in EACH variable
(with cleared overhead)

example:
raw data: VoidPointer shows-> word1(16bit),byte1(8bit),dword1(32bit)...

filled struct:  VarWord= first 32 bit of raw data (16msBits cleared)
                VarByte= next 32 bit of raw data  (24msBits cleared)

but i want such data in struct:
                VarWord==word1(16bit)
                VarByte==byte1( 8bit)
                VarDword==dword1(32bit)
                ..... etc.

it's for easy reading, puffering ,altering, and storing
of not aligned file headers..(lots of grafic-fileheaders)


ps:     i tryed direct pointeraritmetic,cast for each variable,
        create byte-pointers and tryed to cast to byte/word/dword..
        and other crazy things..
        but i dont want to read the haeder as bytes, and calculate
        the right values later, i want to fill the struct direct.
        __notaligned (right keyword?) is detected, but not supported
        by msvc4.0 (error: keyword not further supported)
note:   there exist a bitmapheader defined in a mfc-source        
(tagBitmapHeader) via typedef struct {...};
        and this struct is not aligned(!!) for example.
        but if i use the same kind of declaration in my own code,
        (copyed..)  the resultin struct is aligned.. and become useles  for me.



Tue, 31 Aug 1999 03:00:00 GMT  
 problem with alignment of struct member datas (MSVC 4.2)

Mathias,

In MSVC Developer studio, drop down the Build Settings, expand the project
and select the file you want to switch OFF alignment. Go to the C++ tab and
option code-generation and selection 1-byte alignment.



Tue, 31 Aug 1999 03:00:00 GMT  
 problem with alignment of struct member datas (MSVC 4.2)

Quote:

> hi!

> i've a little problem with alignment of structs includes
> BYTE WORD and DWORD sized variables.
> all of these variables are aligned on DWORD bounds, but in this case,
> i can't mask (and read/write) a non-aligned void-pointed data structure.
> HOW CAN I SWITCH OF VARIABLE-ALIGNMENT?? (ONLY IN SMALL CODE-PARTS!)
> Shure, I could write this part in assemler, but i've to port the code,
> and I dont like assembler for type casting.. (unreadable..)
> In borlandc++ i know some switchs and castings, but they are all invalid
> in msvc 4.0 .

> please send a sample with your sollution to:

> THANK YOU!!

> -> struct Header
>         {
>         WORD VarWord;
>         BYTE VarByte;
>         DWORD VarDword;
>         ... // 50 other variables with BYTE/WORD and DWOR sizes
>         };

> if i save raw, untyped data in this struct:
> ->      Header m_Header= *(Header*)VoidPointer;

> only DWORD parts of raw data are stored in EACH variable
> (with cleared overhead)

> example:
> raw data: VoidPointer shows-> word1(16bit),byte1(8bit),dword1(32bit)...

> filled struct:  VarWord= first 32 bit of raw data (16msBits cleared)
>                 VarByte= next 32 bit of raw data  (24msBits cleared)

> but i want such data in struct:
>                 VarWord==word1(16bit)
>                 VarByte==byte1( 8bit)
>                 VarDword==dword1(32bit)
>                 ..... etc.

> it's for easy reading, puffering ,altering, and storing
> of not aligned file headers..(lots of grafic-fileheaders)


> ps:     i tryed direct pointeraritmetic,cast for each variable,
>         create byte-pointers and tryed to cast to byte/word/dword..
>         and other crazy things..
>         but i dont want to read the haeder as bytes, and calculate
>         the right values later, i want to fill the struct direct.
>         __notaligned (right keyword?) is detected, but not supported
>         by msvc4.0 (error: keyword not further supported)
> note:   there exist a bitmapheader defined in a mfc-source
> (tagBitmapHeader) via typedef struct {...};
>         and this struct is not aligned(!!) for example.
>         but if i use the same kind of declaration in my own code,
>         (copyed..)  the resultin struct is aligned.. and become useles  for me.

I guess,  you could also use
#pragma pack  compiler directive
some thing like this

#pragma pack(1) // one byte aligned => No byte spaces between two
contiguous members
struct _tagMystruct
{
  .. whatever ...

Quote:
};

#pragma pack() // restores to default byte packing

optionally you could add a filler or properly sequence the
variables
 something like
 struct
 {
  WORD w;
  DWORD dw;
  BYTE  byte;
  BYTE  byteFiller;  // optional
 };

This would make your struct members your machine word aligned
( aligned at memory address which are multiples of WORD size for your
machine
(no. of ones in ~0))
and could give you optimal performance.
Regards,
Gopal



Wed, 01 Sep 1999 03:00:00 GMT  
 problem with alignment of struct member datas (MSVC 4.2)



Quote:
>i've a little problem with alignment of structs includes
>BYTE WORD and DWORD sized variables.
>all of these variables are aligned on DWORD bounds, but in this case,
>i can't mask (and read/write) a non-aligned void-pointed data structure.

True, but you can work around that...

Quote:
>if i save raw, untyped data in this struct:
>->      Header m_Header= *(Header*)VoidPointer;

Instead of trying to cast and dereference, try:
  #include <string.h>
  /*...*/
    Header m_Heaader;
    memcpy(&m_header, VoidPointer, sizeof m_header);

                --Ken Pizzini



Thu, 02 Sep 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Problem with "Struct member alignment".

2. Question about: Memory usage / Struct alignment (simple C program with MSVC)

3. MSVC 4.2 Problems (MFC/non MFC)

4. new problem with msvc 4.2 and odbc recordset

5. MSVC 4.2 watch problem

6. Performance problems with MSVC 4.2

7. struct alignment (char members)

8. Member alignment requirement for struct copying

9. HELP: struct data members alignment???

10. struct member alignment

11. struct member alignment

12. Changes between MSVC 4.2 and 5.0

 

 
Powered by phpBB® Forum Software