 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Beta Jin Guest
|
Posted: Wed Sep 24, 2003 8:20 pm Post subject: How to determinate the aligned size for a type in C++ |
|
|
My current implementation is,
template <typename Type>
class get_alignment_t
{
Type m_dummy[2] ;
public:
enum { result = sizeof(m_dummy)/2 };
};
I am not quite sure if my implementation is suitable for all types.
Any advice or reference link is appreciated.
Beta
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Glen Low Guest
|
Posted: Thu Sep 25, 2003 1:31 pm Post subject: Re: How to determinate the aligned size for a type in C++ |
|
|
| Quote: | I am not quite sure if my implementation is suitable for all types.
Any advice or reference link is appreciated.
Beta
|
According to 5.3.3/2, when sizeof is applied to an array of n
elements, it always returns n * sizeof the element. There is no
padding or realignment for arrays, so you won't get an alignment out
of that.
C++ may insert padding in structures though, if that's what you're
after. You could try declaring a structure containing 2 fields of that
type, get the sizeof / 2.
Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Sep 25, 2003 1:33 pm Post subject: Re: How to determinate the aligned size for a type in C++ |
|
|
"Beta Jin" <betajin (AT) hotmail (DOT) com> wrote
| Quote: | My current implementation is,
template
class get_alignment_t
{
Type m_dummy[2] ;
public:
enum { result = sizeof(m_dummy)/2 };
};
I am not quite sure if my implementation is suitable for all types.
Any advice or reference link is appreciated.
|
Your code is not correct. It also will never return anything other
than sizeof (Type). The padding necessary to make an array work
of something is already accounted for in the sizeof the single
object.
The sizeof will frequently be bigger than the alignment requirements.
struct Test {
double d[100];
};
is going to return 100*sizeof(double) where the alignment constraint
is probably no worse than sizeof(double).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Attila Feher Guest
|
Posted: Thu Sep 25, 2003 1:45 pm Post subject: Re: How to determinate the aligned size for a type in C++ |
|
|
Beta Jin wrote:
| Quote: | My current implementation is,
template
class get_alignment_t
{
Type m_dummy[2] ;
public:
enum { result = sizeof(m_dummy)/2 };
};
I am not quite sure if my implementation is suitable for all types.
Any advice or reference link is appreciated.
|
sizeof(Type) it is. Why, you may ask? Because this is true:
T arr[N];
sizeof(arr)/sizeof(T) = N;
so:
sizeof(T[N])/sizeof(T) = N; / *sizeof(T)
sizeof(T[N]) = N*sizeof(T);
So your sizeof(m_dummy)/2 above translates to:
sizeof(T[N])/N
after substitution it is:
N*sizeof(T)/N
after simplification it is:
sizeof(T)
So:
sizeof(m_dummy)/2 == sizeof(Type)
And we know that sizeof(m_dummy)/2 is the good, aligned size.
BUT. This aligned size is ONLY for the this very type aligned to itself!
struct Ajve {
char c;
int i;
};
sizeof(Ajve) != sizeof(char)+sizeof(i); // !!!
--
Attila aka WW
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Mon Sep 29, 2003 10:10 pm Post subject: Re: How to determinate the aligned size for a type in C++ |
|
|
Beta Jin wrote:
| Quote: | My current implementation is,
template <typename Type
class get_alignment_t
{
Type m_dummy[2] ;
public:
enum { result = sizeof(m_dummy)/2 };
};
|
Doesn't work. Remember the idiom:
T a[] = { .... };
const unsigned num_elts = sizeof(a)/sizeof(a[0]);
What you're looking for is the amount of extra space that
the compiler may allocate between a T and something that
is not aligned.
Here is an approach:
template
struct A {
char x; // usually aligned at 1.
// Assume alignment padding padding goes here
T t;
// and maybe here.
};
#define offset_of(type, field)
(unsigned((& (((type *)0)->field)))
now: with sizeof(T), sizeof(A<T>), offset_of(A<T>, x), and
offset_of(A<T>, t) you can figure out the exact layout of
the struct A<T> and any padding around the member t.
The macro offset_of() is an approximation of a "real" macro
hidden somewhere in the standard, or quasi-standard, libraries.
Note that this approach has issues: for example, the template
A<T> assumes a bunch of things about T's constructors,
destructor, and assignment.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Shay Guest
|
Posted: Tue Sep 30, 2003 10:43 am Post subject: Re: How to determinate the aligned size for a type in C++ |
|
|
Antoun Kanawati wrote:
[snip]
| Quote: | template<typename T
struct A {
char x; // usually aligned at 1.
// Assume alignment padding padding goes here
T t;
// and maybe here.
};
#define offset_of(type, field)
(unsigned((& (((type *)0)->field)))
now: with sizeof(T), sizeof(A<T>), offset_of(A<T>, x), and
offset_of(A<T>, t) you can figure out the exact layout of
the struct A<T> and any padding around the member t.
[snip]
Note that this approach has issues: for example, the template
A<T> assumes a bunch of things about T's constructors,
destructor, and assignment.
|
I hadn't come up with this idea before. I like it because it avoids the
likely over-estimation inherent in assuming that a type's alignment
requirement is no less than the type's size.
Dealing with T's constructor is simple: just provide a declaration (and no
definition) for a default constructor for A. Since an A object isn't ever
created, the lack of a definition for its constructor isn't a problem.
template<typename T>
struct A {
char x;
T t;
A(); // never defined
};
I think offsetof is only defined for POD types. A workaround would be to
find how much bigger A<T> is than T:
template<typename T>
struct alignment_requirement {
static size_t const result = sizeof (A<T>) - sizeof (T);
};
--
Shay
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|