tags:

views:

30

answers:

1

Hi All,

I have a code that uses #pragma pack(push,8) but it does not seem to take effect somehow but I can't figure out what's causing this problem.

For example, look at the following code.

#include <windows.h>
#include <stdio.h>

#pragma pack(push, 8)

typedef struct _MY_DATA {
 LARGE_INTEGER a;
 LARGE_INTEGER b;
 ULONG count;
} MY_DATA;

#pragma pack(show)
#pragma pack(pop)

int main()
{
 MY_DATA data;

 printf("data size:%d\n", sizeof(data));

 return 0;
}

This would return "data size:24" but I use the same code in other application that is managed by Visual Studio and there I am getting "data size:20".

So I am assuming it has to do with some settings but could not figure out. I will really appreciate it if anybody could give me some hint. Thanks.

A: 

The project default setting can have higher precedence than the pragma - see this MSDN article.

Structure packing interacts with compiler alignment behavior as follows.

  • If the packsize is set equal to or greater than the default alignment, the packsize is ignored.
  • If the packsize is set smaller than the default alignment, the compiler aligns according to the packsize value.
Pete Kirkham