By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Structure Padding

P: 22
how to find padding done by compiler for a structure?
For ex:
struct
{
int i1;
char c1;
int i2;
} pad;

here the sizeof(pad) will be 12. Now i want to know is there any method/program to find out compiler output (*)for padding?

(*) struct
{
int i1;
char c1;
paddin;
paddin;
paddin;
int i2;
} pad;
Jun 20 '07 #1
Share this Question
Share on Google+
2 Replies


P: 18
Padding (or packing) is a compiler option.

What compiler are you using? It can also be controlled using #pragma directive.

Most compilers have default value of 8 (field are aligned on 8 bytes boundaries)

So:

struct S
{
double d1;
char c;
double d2;
};

will have sizeof(S) == 24;

Look for:

#pragma pack

Or maybe something like:

__declspec(align(32)) struct Str1{
int a, b, c, d, e;
};

At http://msdn2.microsoft.com/en-us/lib...65(VS.80).aspx

And also __alignof operator exists but is it Microsoft specific (probably VS2005 or more)
Jun 20 '07 #2

P: 22
Hi,
Actually i don't want to modify structure, i just want to know that is their any to find compiler padded structure as output.
Ex: struct{ int a; char b; int c;} .. now after padding the structure will be
struct{ int a; char b;pad1; pad2; int c;}.. i actually want this as an output.


Padding (or packing) is a compiler option.

What compiler are you using? It can also be controlled using #pragma directive.

Most compilers have default value of 8 (field are aligned on 8 bytes boundaries)

So:

struct S
{
double d1;
char c;
double d2;
};

will have sizeof(S) == 24;

Look for:

#pragma pack

Or maybe something like:

__declspec(align(32)) struct Str1{
int a, b, c, d, e;
};

At http://msdn2.microsoft.com/en-us/lib...65(VS.80).aspx

And also __alignof operator exists but is it Microsoft specific (probably VS2005 or more)
Jun 25 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.