Packing bools with a bit field (C ++)

I am trying to interact with Ada code using C ++, so I am defining the structure using bit fields, so that all the data is in the same place in both languages. The following is not exactly what I am doing, but outlines the problem. The following is also a console application in VS2008, but this is not very important.

using namespace System;
int main() {
    int array1[2] = {0, 0};
    int *array2 = new int[2]();
    array2[0] = 0;
    array2[1] = 0;

    #pragma pack(1)
    struct testStruct {
        // Word 0 (desired)
        unsigned a : 8;
        unsigned b : 1;
        bool c : 1;
        unsigned d : 21;
        bool e : 1;

        // Word 1 (desired)
        int f : 32;

        // Words 2-3 (desired)
        int g[2]; //Cannot assign bit field but takes 64 bits in my compiler
    };
    testStruct test;

    Console::WriteLine("size of char: {0:D}", sizeof(char) * 8);
    Console::WriteLine("size of short: {0:D}", sizeof(short) * 8);
    Console::WriteLine("size of int: {0:D}", sizeof(int) * 8);
    Console::WriteLine("size of unsigned: {0:D}", sizeof(unsigned) * 8);
    Console::WriteLine("size of long: {0:D}", sizeof(long) * 8);
    Console::WriteLine("size of long long: {0:D}", sizeof(long long) * 8);
    Console::WriteLine("size of bool: {0:D}", sizeof(bool) * 8);
    Console::WriteLine("size of int[2]: {0:D}", sizeof(array1) * 8);
    Console::WriteLine("size of int*: {0:D}", sizeof(array2) * 8);
    Console::WriteLine("size of testStruct: {0:D}", sizeof(testStruct) * 8);
    Console::WriteLine("size of test: {0:D}", sizeof(test) * 8);

    Console::ReadKey(true);

    delete[] array2;
    return 0;
}

(If this was unclear, in a real program, the main idea is that the program receives null*from something related to the Ada code and passes it in testStruct*to access the data, I think.)

C #pragma pack(1)commented out:

size of char: 8
size of short: 16
size of int: 32
size of unsigned: 32
size of long: 32
size of long long: 64
size of bool: 8
size of int[2]: 64
size of int*: 32
size of testStruct: 224
size of test: 224

Obviously, 4 words (with an index of 0-3) should be 4 * 4 * 8 = 32 * 4 = 128 bits, not 224. Other output lines should help confirm the type size in the VS2008 compiler.

#pragma pack(1) ( ) 176, - 128. , bools unsigned ints " 0".

: a & b, c, d, e, f, , 5, +2 = 7 , 32 = 224, , #pragma pack(1) . c e (bools) 8 , 32, 176, , #pragma pack(1) . , #pragma pack(1) bools , , bools ints .

, , : a e ? : ++ bools, ; , .

, - ? , :

  • , ( ).
  • bools unsigned ints, , bool , , , , , .
  • private unsigned ints, -, (), , IntelliSense , , (), .
  • (, , testStruct, testStructImport -type), .
+4
4

. , #pragma, . bool unsigned int , . f f_flag f_bool, , . , , , , - .

0

"" - . POD .

"" (uint8_t) .

. . . - .

. , Endianess .

, - .

+5

, , @WaltK :

"If you want more control over the arrangement of bitfield structures in memory, consider using this bitfield tool, implemented as a library header file ."

0
source

Try packing this way:

    #pragma pack( push, 1 )
struct testStruct {
    // Word 0 (desired)
    unsigned a : 8;
    unsigned b : 1;
    unsigned c : 1;
    unsigned d : 21;
    unsigned e : 1;

    // Word 1 (desired)
    unsigned f : 32;

    // Words 2-3 (desired)
    unsigned g[2]; //Cannot assign bit field but takes 64 bits in my compiler
};
#pragma pack(pop)
-1
source

Source: https://habr.com/ru/post/1548537/


All Articles