Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV

Before Posting...

Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


Byte Alignment

OSR_Community_UserOSR_Community_User Member Posts: 110,217
Hello All,

Does anybody know how to make the settings for the BYTE ALIGNMENT /PACKING
for the structures.
If yes, what are the settings for it.??

The problem we are facing is

typedef struct
{
unsigned char VAR1[3];
}A;

typedef struct
{
A a; // 3- BYTE SIZE
unsigned int VAR2:8; // 1- BYTE SIZE
unsigned int VAR3:16; // 2- BYTE SIZE
}B;

The sizeof (A) comes out to be 3
and sizeof(B) comes out to be 7 (instead of 6).

This is because the VC compiler stuffs a byte after the first structure A.
Is there any settings for the alignment etc. to solve this .

Thanks

Rakesh Shrivastava
DCM Tech
New Delhi

Comments

  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    Lookup the pragma directive "#pragma pack " in the SDK

    Rohitab Batra
    [email protected]
    http://www.opengroup.org
    ------------------------------------------
    f u cn rd ths u cn gt a gd jb n cmptr pgmg


    -----Original Message-----
    From: Rakesh Shrivastava
    Sent: Saturday, April 29, 2000 2:51 PM
    To: NT Developers Interest List
    Subject: [ntdev] Byte Alignment


    Hello All,

    Does anybody know how to make the settings for the BYTE ALIGNMENT /PACKING
    for the structures.
    If yes, what are the settings for it.??

    The problem we are facing is

    typedef struct
    {
    unsigned char VAR1[3];
    }A;

    typedef struct
    {
    A a; // 3- BYTE SIZE
    unsigned int VAR2:8; // 1- BYTE SIZE
    unsigned int VAR3:16; // 2- BYTE SIZE
    }B;

    The sizeof (A) comes out to be 3
    and sizeof(B) comes out to be 7 (instead of 6).

    This is because the VC compiler stuffs a byte after the first structure A.
    Is there any settings for the alignment etc. to solve this .

    Thanks

    Rakesh Shrivastava
    DCM Tech
    New Delhi









    ---
    You are currently subscribed to ntdev as: [email protected]
    To unsubscribe send a blank email to $subst('Email.Unsub')
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    From the VC docs:

    pack
    #pragma pack( [ n] )

    Specifies packing alignment for structure and union members. Whereas the
    packing alignment of structures and unions is set for an entire translation
    unit by the /Zp option, the packing alignment is set at the data-declaration
    level by the pack pragma. The pragma takes effect at the first structure or
    union declaration after the pragma is seen; the pragma has no effect on
    definitions.

    When you use #pragma pack(n), where n is 1, 2, 4, 8, or 16, each structure
    member after the first is stored on the smaller member type or n-byte
    boundaries. If you use #pragma pack without an argument, structure members
    are packed to the value specified by /Zp. The default /Zp packing size is
    /Zp8.

    Jim

    -----Original Message-----
    From: [email protected]
    [mailto:[email protected]]On Behalf Of [email protected]
    Sent: Saturday, April 29, 2000 2:21 AM
    To: NT Developers Interest List
    Subject: [ntdev] Byte Alignment


    Hello All,

    Does anybody know how to make the settings for the BYTE ALIGNMENT /PACKING
    for the structures.
    If yes, what are the settings for it.??

    The problem we are facing is

    typedef struct
    {
    unsigned char VAR1[3];
    }A;

    typedef struct
    {
    A a; // 3- BYTE SIZE
    unsigned int VAR2:8; // 1- BYTE SIZE
    unsigned int VAR3:16; // 2- BYTE SIZE
    }B;

    The sizeof (A) comes out to be 3
    and sizeof(B) comes out to be 7 (instead of 6).

    This is because the VC compiler stuffs a byte after the first structure A.
    Is there any settings for the alignment etc. to solve this .

    Thanks

    Rakesh Shrivastava
    DCM Tech
    New Delhi









    ---
    You are currently subscribed to ntdev as: [email protected]
    To unsubscribe send a blank email to $subst('Email.Unsub')
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    Or you can use compiler independent method:

    #include "pshpackX.h" where X is required alignment (e.g. 1, 2, 4,
    ...)
    #include "poppack.h"

    These include files come with Platform SDK and for example NTIFS.H uses
    them.

    > -----P?vodn? zpr?va-----
    > Od: [email protected] [SMTP:[email protected]]
    > Odesl?no: 29. dubna 2000 11:51
    > Komu: NT Developers Interest List
    > P?edm?t: [ntdev] RE: Byte Alignment
    >
    > Lookup the pragma directive "#pragma pack " in the SDK
    >
    > Rohitab Batra
    > [email protected]
    > http://www.opengroup.org
    > ------------------------------------------
    > f u cn rd ths u cn gt a gd jb n cmptr pgmg
    >
    >
    > -----Original Message-----
    > From: Rakesh Shrivastava
    > Sent: Saturday, April 29, 2000 2:51 PM
    > To: NT Developers Interest List
    > Subject: [ntdev] Byte Alignment
    >
    >
    > Hello All,
    >
    > Does anybody know how to make the settings for the BYTE ALIGNMENT /PACKING
    > for the structures.
    > If yes, what are the settings for it.??
    >
    > The problem we are facing is
    >
    > typedef struct
    > {
    > unsigned char VAR1[3];
    > }A;
    >
    > typedef struct
    > {
    > A a; // 3- BYTE SIZE
    > unsigned int VAR2:8; // 1- BYTE SIZE
    > unsigned int VAR3:16; // 2- BYTE SIZE
    > }B;
    >
    > The sizeof (A) comes out to be 3
    > and sizeof(B) comes out to be 7 (instead of 6).
    >
    > This is because the VC compiler stuffs a byte after the first structure A.
    > Is there any settings for the alignment etc. to solve this .
    >
    > Thanks
    >
    > Rakesh Shrivastava
    > DCM Tech
    > New Delhi
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > ---
    > You are currently subscribed to ntdev as: [email protected]
    > To unsubscribe send a blank email to $subst('Email.Unsub')
    >
    > ---
    > You are currently subscribed to ntdev as: [email protected]
    > To unsubscribe send a blank email to $subst('Email.Unsub')
  • Mark_ShnaiderMark_Shnaider Member Posts: 95
    Try:
    #pragma pack(1)
    typedef struct
    {
    unsigned char VAR1[3];
    }A;
    #pragma pack()
    Mark

    [email protected] wrote:

    > Hello All,
    >
    > Does anybody know how to make the settings for the BYTE ALIGNMENT /PACKING
    > for the structures.
    > If yes, what are the settings for it.??
    >
    > The problem we are facing is
    >
    > typedef struct
    > {
    > unsigned char VAR1[3];
    > }A;
    >
    > typedef struct
    > {
    > A a; // 3- BYTE SIZE
    > unsigned int VAR2:8; // 1- BYTE SIZE
    > unsigned int VAR3:16; // 2- BYTE SIZE
    > }B;
    >
    > The sizeof (A) comes out to be 3
    > and sizeof(B) comes out to be 7 (instead of 6).
    >
    > This is because the VC compiler stuffs a byte after the first structure A.
    > Is there any settings for the alignment etc. to solve this .
    >
    > Thanks
    >
    > Rakesh Shrivastava
    > DCM Tech
    > New Delhi
    >
    > ---
    > You are currently subscribed to ntdev as: [email protected]
    > To unsubscribe send a blank email to $subst('Email.Unsub')
  • Gregory_G._DyessGregory_G._Dyess Member - All Emails Posts: 382
    The long answer is this:
    #pragma pack( push, <Name>, <Packing size>)
    and to undo it/return to default:
    #pragma pack( pop, <Name> )


    Look up #pragma pack in the docs and it will explain the optional parameters
    shown above.

    Greg

    -----Original Message-----
    From: [email protected]
    [mailto:[email protected]]On Behalf Of [email protected]
    Sent: Saturday, April 29, 2000 4:21 AM
    To: NT Developers Interest List
    Subject: [ntdev] Byte Alignment


    Hello All,

    Does anybody know how to make the settings for the BYTE ALIGNMENT /PACKING
    for the structures.
    If yes, what are the settings for it.??

    The problem we are facing is

    typedef struct
    {
    unsigned char VAR1[3];
    }A;

    typedef struct
    {
    A a; // 3- BYTE SIZE
    unsigned int VAR2:8; // 1- BYTE SIZE
    unsigned int VAR3:16; // 2- BYTE SIZE
    }B;

    The sizeof (A) comes out to be 3
    and sizeof(B) comes out to be 7 (instead of 6).

    This is because the VC compiler stuffs a byte after the first structure A.
    Is there any settings for the alignment etc. to solve this .

    Thanks

    Rakesh Shrivastava
    DCM Tech
    New Delhi









    ---
    You are currently subscribed to ntdev as: [email protected]
    To unsubscribe send a blank email to $subst('Email.Unsub')
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    > Does anybody know how to make the settings for the BYTE ALIGNMENT /PACKING
    > for the structures.

    #pragma pack(1)
    - (in the source, surely) turns off alignment.
    #pragma pack()
    - turns it back on.

    Max
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Kernel Debugging 30 January 2023 Live, Online
Developing Minifilters 20 March 2023 Live, Online
Writing WDF Drivers TBD 2023 Live, Online
Internals & Software Drivers 17 April 2023 Live, Online