Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Struct packing bug (?)

Michael_Ljunggren
392 Views

Hi,

Downloaded the eval version of 11.1 for Windows the other day and run into some problems. Certain structures in our programs need to be packed tightly for storage on disk. Version 9.1 and I think 10.1 pack our structures according to historic sizes.

But 11.1 can't seem to do it. I think I have tried every concievable way to adjust the structure size but it just won't work. Is this a known bug? Should I use a different kind of compiler switch?

Example:

struct

{

double a;

int b;

int c[8];

}

should have the size 44 bytes. How to make it happen?

Help would be very much appreciated, as we are facing going back to 9.1... Thanks!

0 Kudos
5 Replies
JenniferJ
Moderator
392 Views

You can use "/Zp specify alignment constraint for structures (n=1,2,4,8,16)" option to control it.

Or

[bash]#pragma pack(4)
typedef struct {
    double a;
    int b;
    int c[8];
} strtType_myData;
#pragma pack()[/bash]

Maybe move "int b" after "int c[8]" like below so all variables are aligned by 8 bytes.

[bash]#pragma pack(4)
typedef struct {
    double a;
    int c[8];
    int b;
} strtType_myData;
#pragma pack()[/bash]

Jennifer

0 Kudos
Judith_W_Intel
Employee
392 Views

It's not a bug if we lay out classes the same way as Microsoft (it would be a bug if we didn't because we have to be link compatible).

Another similar optionto what Jennifer described above isto use the pop/push specifiers to pragma pack, i.e.:

#pragma pack(push,4)
struct S
{
double a;
int b;
int c[8];
};
#pragma pack(pop)

0 Kudos
Michael_Ljunggren
392 Views

Thank you Jennifer. After doing more tests I realize I lied to you. I did not use double's and int's directly. I used a containing class with the identical size like this:


class D{ double storage; }

class I { int storage;}

and then:

struct

{

D a;

I b

I c[8];

}

When using structs with int & double directly, things work. But when using the pseudo variables it won't work properly. The size becomes 48 bytes in all cases. There is no doubt that the containing classes have the proper size, namely exactly that of the storage variable. It is when combining these containing classes in a struct that it fails when I combine such of different sizes. (Apparently...)

Does anyone have any insight here? Is struct packing not defined the same way when you try to pack POD classes?

0 Kudos
Michael_Ljunggren
392 Views

Anyone?

Is struct packing not defined the same way with classes as with variables?

0 Kudos
JenniferJ
Moderator
392 Views

The same pragma works for class as well.

a.cpp:

[bash]>>type a.cpp
#include 

class D { double storage; };
class I { int storage;};

#pragma pack(push,4)
typedef struct {
        D a;
        I b;
        I c[8];
} stDataClass_Pack;
#pragma pack(pop)

typedef struct {
        D a;
        I b;
        I c[8];
} stDataClass;

#pragma pack(push,4)
typedef struct {
    double a;
    int b;
    int c[8];
} stData_Pack;
#pragma pack(pop)

typedef struct {
    double a;
    int b;
    int c[8];
} stData;

int main(int argc, char* argv[])
{
        stDataClass_Pack dClass_Pack;
        stDataClass dClass;
        stData_Pack dData_Pack;
        stData dData;

        printf("sizeof(dDataClass_Pack): %dn", sizeof(dClass_Pack));
        printf("sizeof(dDataClass): %dn", sizeof(dClass));
        printf("sizeof(dData_Pack): %dn", sizeof(dData_Pack));
        printf("sizeof(dData): %dn", sizeof(dData));
        return 0;
}
[/bash]

output:

>>icl /O2 a.cpp
Intel C++ Compiler Professional for applications running on IA-32, Version 11.1 Build 20091130 Package ID: w_cproc_p_11.1.054
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.

a.cpp
Microsoft Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.

-out:a.exe
a.obj

C:\temp
>>a
sizeof(dDataClass_Pack): 44
sizeof(dDataClass): 48
sizeof(dData_Pack): 44
sizeof(dData): 48

0 Kudos
Reply