Summerpinn
Summerpinn

Reputation: 35

Better way to refer to structure size( #pragma pack VS defined size )

Assume I have the struct Foo

struct Foo{
    int a;
    short b;
    char  c;
};

And I has to write this struct in to n/w buffer. I need to know the size of this struct. In my environment, sizeof(Foo) returns 8, That's ok, I understand.

These are two way to refer to the exact size that I want to write to the buffer

1.#define SIZEOF_FOO 7, And use this instead of sizeof(Foo)
2. By using #pragma pack so I can use sizeof(Foo) which will return 7

Which one is the better way to do, or Is there any other way to do this ?

Upvotes: 1

Views: 2090

Answers (5)

Vikram.exe
Vikram.exe

Reputation: 4585

#define SIZEOF_FOO 7 without using #pragma pack should not be used at all. The reason being, when you call send (or any equivalent) function passing a variable of your struct type and pass the data size as 7, the send function will send only the first 7 bytes of data, and when you receive this data at the other end, you will definitely mess a lot of things in your code.

Using #pragma pack directives will change the alignment of member of your struct amd you will certainly save a few bytes of data over network transfer, but if you think in terms of optimization/performance, these few bytes are negligible. Although, most of the applications (that I have seen) use #pragma pack for packing struct members, but that totally depends on your requirement.

One major problem with using a #define SIZEOF_FOO 7 is that, if you port your code to some other platform/architecture, the actual size of your structure might not be same (7 bytes) and then you will either have to use a platform dependent macro or come up with some other alternative.

So in short, you should always use sizeof instead of using #define SIZEOF_FOO.

Upvotes: 0

Michael Burr
Michael Burr

Reputation: 340168

If you need to write out such a structure to a network, please use a protocol framework or marshal the data explicitly using a protocol you define independent of the structure layout.

Depending on the binary layout details of a structure will likely cause pain down the road.

I've heard good things about Google Protocol Buffers and Boost serialization, but I haven't used either.

I've dealt with a protocol defined in a document, and output the data according to that protocol using custom marshaling routines, or used an IDL description (which is kind of like an annotated C structure that used to drive a code generator that crates the serialization/deserialization routines).

That way you (or the protocol framework) control the use of padding regardless of what the compiler does to the structure. Note that is you use a protocol framework, you typically need to use it on both ends (which might be a problem if the other node is out of your control or an embedded device which the framework doesn't support, for example).

Upvotes: 2

Mike Caron
Mike Caron

Reputation: 14561

This question is severely misguided. The size of Foo is always whatever sizeof(Foo) returns. Do not assume, do not use wishful thinking, use sizeof.

In fact, I would expect sizeof(Foo) to be 12, aligning everything on a 4-byte boundary.

Either way, the good news is that sizeof is resolved at compile time, and has absolutely no impact on performance. So, there's no reason not to use it.

#define SIZEOF_FOO (sizeof(Foo))

Upvotes: 0

maxim1000
maxim1000

Reputation: 6365

I would advice to write structure by sequential writing its members. E.g. you can use boost serialization.

This will separate representation of the structure in application memory from format used for transmission.

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361254

The #pragma pack directive modifies the current alignment rule for members of structures following the directive. Hence, it affects the size of structs defined after the pragma!

You most likely don't need this. Just use sizeof(Foo) wherever you want to know the size of your struct. And don't write pragmas, as they are used in some special cases!

You can see these links to know in details about pragmas:

GCC - Structure-Packing Pragmas
MSVC++ - Pack
pragma pack

Upvotes: 0

Related Questions