tags:

views:

283

answers:

5

I understand the structure alignment is 'implementation specific', but just wondering if there is any simple way to calculate the structure alignment, for example:

typedef struct
{
    char c;
    int  i;
} test;

if sizeof(test) - (sizeof(char) + sizeof(int)) == 0 means alignment is  1 byte;
if sizeof(test) - (sizeof(char) + sizeof(int)) == 1 means alignment is  2 bytes;
// ...etc...

Is the above assumption reliably true?

A: 

If all you want is a good first guess, then transitively find the maximum size of the largest non-aggregate member in the structure and any nested structures

DigitalRoss
+3  A: 

Your method is not reliable in the general case: if you tried it on this struct:

typedef struct
{
    int i1;
    int i2;
} test;

it would give you zero as the result, which is probably not the alignment.

Here is a method that works, though:

#include <stddef.h>

struct dummy
{
    char x;
    test t;
};

size_t test_alignment = offsetof(struct dummy, t);
caf
#include <stddef.h>struct dummy{ char x; int i;};size_t test_alignment = offsetof(struct dummy, i);does above work?
c-stranger
Depends on what you mean by "works". It sets test_alignment to the alignment of `i` inside `struct dummy`. If that's what you want it to do, then it certainly does work.
Chris Lutz
That would test the alignment of type `int` (I assumed you wanted to test the alignment of the defined type `test`?)
caf
A: 

Hi Caf, thanks for your post, but does the following work?

#include <stddef.h>

struct dummy
{
    char x;
    int  i;
};

size_t test_alignment = offsetof(struct dummy, i);
c-stranger
Generally speaking, it's better to wait more than two minutes before resorting to more drastic measures. Keep in mind that caf posted over half an hour ago. Don't expect him, or anyone, to answer your questions immediately. Give us a little time.
Chris Lutz
I expect that c-stranger re-posted the comment as an answer because the formatting was terrible in the comment.
Josh Matthews
Yes, that would work to test the alignment of type `int`. It is the same as doing `typedef int test;` in the code I gave (which was for determining the alignment of defined type `test`).
caf
A: 

And here's the macro I have in my toolbox (using caf's technique) to get the alignment of a type:

#define ALIGNMENT_OF(t) offsetof( struct { char x; t dummy; }, dummy)

So now you can use such expressions as:

typedef struct
{
    char c;
    int  i;
} test;

size_t x = ALIGNMENT_OF( test);    // will likely set x = 4

Note that a particular compiler might support a non-standard intrinsic that you might prefer to use (for example, MSVC at some point started supporting an __alignof() intrinsic). My header defines ALIGNMENT_OF() using that intrinsic if it detects a compiler that supports it (though I honestly don't know what advantage the intrinsic provides).

Michael Burr
If I'm not mistaken, your macro has undefined behavior because `offsetof` is not required to work on anonymous structs passed as an argument like that. You can fix it by writing out the portable definition of `offsetof` yourself in the macro body.
R..
A: 

Make alligned structures

#pragma pack(1)
struct tight{           
   short element_1;       
   int *element_2;
};
#pragma pack(pop)
ralu