Play and learn 300 000+ tabs online

Thursday, June 3, 2010

How to generate Alignment error in C - Padding



#pragma pack (1)
struct s {
char c; // offset 0
int i; // offset 1!
} ss;
#pragma pack ()

void f_improper(int *p)
{
*p = 23; // generates a fault
}

void g_proper(int __unaligned *p) // OK
{
*p = 42;
}

void main ()
{
f_improper(&ss.i);
g_proper(&ss.i);
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.