468,315 Members | 1,408 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

can someone explain max_aligh union mentioned Sutter's book

Why Attempt #3 is Deplorable

First, let's consider a few reasons why Attempt #3 above truly is
deplorable form:

1. Alignment. Unlike dynamic memory acquired by ::operator new(), the
x_ character buffer isn't guaranteed to be properly aligned for
objects of type X. To make this work more reliably, the programmer
could use a "max_align" union, which is usually implemented as
something like:

union max_align {
short dummy0;
long dummy1;
double dummy2;
long double dummy3;
void* dummy4;
/*...and pointers to functions, pointers to
member functions, pointers to member data,
pointers to classes, eye of newt, ...*/
};

It would be used like this:

union {
max_align m;
char x_[sizeofx];
};

I don't understand how this will fix the alignment issue.

Thanks
Jun 27 '08 #1
2 1005
I don't understand how this will fix the alignment issue.

You don't say what book you're referring to, so I don't
know what "attempt #3" looks like, but I will note that
if you have a structure like:

struct S {
char m[sizeof(int)];
};

the compiler will likely feel justified putting it at
any alignment it wants, but if have a structure like:

struct S {
int m;
};

it will *always* put it at alignments suitable for m to
be accessed by the CPU (usually at N-byte boundaries,
where N is sizeof(int)).

The union just forces the compiler to align the
object at M-byte boundaries, where M is the largest
alignment-needing-thing in the union.

Sean
Jun 27 '08 #2
"SeanW" <se***********@yahoo.comwrote in message
news:b3**********************************@u69g2000 hse.googlegroups.com...
>I don't understand how this will fix the alignment issue.

You don't say what book you're referring to, so I don't
know what "attempt #3" looks like, but I will note that
if you have a structure like:

struct S {
char m[sizeof(int)];
};

the compiler will likely feel justified putting it at
any alignment it wants, but if have a structure like:

struct S {
int m;
};

it will *always* put it at alignments suitable for m to
be accessed by the CPU (usually at N-byte boundaries,
where N is sizeof(int)).

The union just forces the compiler to align the
object at M-byte boundaries, where M is the largest
alignment-needing-thing in the union.
Here is some crude code that attempts to extract alignment of a given type:
__________________________________________________ _____________________
#include <iostream>

template<typename T>
class align_of {
struct temp {
char m_offset;
T m_object;
};

public:
enum result_e {
c_result = sizeof(temp) sizeof(T) ?
sizeof(temp) - sizeof(T) : sizeof(T)
};
};

struct object1 {
char a[4];
double b[7];
};
struct object2 {
void (*fp1) (void*);
void (*fp2) (void*, int);
char (*fp3) (void*, char);
};

int main() {
std::cout << "char:\t\t" << align_of<char>::c_result << "\n";
std::cout << "short:\t\t" << align_of<short>::c_result << "\n";
std::cout << "int:\t\t" << align_of<int>::c_result << "\n";
std::cout << "long:\t\t" << align_of<long>::c_result << "\n";
std::cout << "double:\t\t" << align_of<double>::c_result << "\n";
std::cout << "float:\t\t" << align_of<float>::c_result << "\n";
std::cout << "object1:\t" << align_of<object1>::c_result << "\n";
std::cout << "object2:\t" << align_of<object2>::c_result << "\n";
return 0;
}
__________________________________________________ _____________________

Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Michael Rozdoba | last post: by
2 posts views Thread by Shaggy Dragon | last post: by
6 posts views Thread by Neil Zanella | last post: by
24 posts views Thread by Noah Roberts | last post: by
10 posts views Thread by piboye | last post: by
30 posts views Thread by Yevgen Muntyan | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.