dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Structure Padding in visual basics tool.

  1. #1
    Join Date
    Jan 2015
    Posts
    100

    Post Structure Padding in visual basics tool.

    We are trying to see the size of the structure its getting 8 bytes,and get padded with remaining bytes we want to use only 4 bytes only 32bit frame. How to make 32 bit frame. so that we want to use only 4 bytes,Can any one help me out in finding the solutions we are using Visual basics tool.


    Code:
    typedef struct STTCADispCtrl
    {
    	unsigned char label;
    	unsigned char sdi:2;
    	unsigned char ia:1;
    	unsigned char As:2;
    	unsigned char Att:1;
    	unsigned char Dtif:1;
    	unsigned int Range:9;
    	unsigned char Ac:2;
    	unsigned char Mil:3;
    	unsigned char ssm:2;
    	unsigned char parity:1;
    }ST_TCAS_MODE_DISP_CTRL;
    
    int main()
    {
    
    	ST_TCAS_MODE_DISP_CTRL display;
    
    	printf("sizeof ST_TCAS_MODE_DISP_CTRL: %d sizeof unsigned int: %d\n", sizeof(display), sizeof(unsigned int));
    
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: Structure Padding in visual basics tool.

    The minimum number of bytes this will pack to is 5 by setting the Struct Member Alignment to 1 byte.

    label 1 byte

    sdi, ia, As, Att, Dtif 1 byte (only 7 bits used)

    make Range short int so 2 bytes but starts on a byte boundary.

    Ac, Mil, sam, ssm, parity 1 byte

    so total for the struct is 5 bytes.

    If you want this packed into 4 bytes, then Range needs to be split into 2 parts - 1 bit char and and 8 bit char giving
    Code:
    	unsigned char label;
    	unsigned char sdi : 2;
    	unsigned char ia : 1;
    	unsigned char As : 2;
    	unsigned char Att : 1;
    	unsigned char Dtif : 1;
    	unsigned char RangeTop : 1;
    	unsigned char RangeBot : 8;
    	unsigned char Ac : 2;
    	unsigned char Mil : 3;
    	unsigned char ssm : 2;
    	unsigned char parity : 1;
    and using 4 bytes.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)