Re: structures align
"SG" <s.gesemann@gmail.com> wrote in message
news:d43cc208-b5d0-4213-bf70-2fd5484b7990@v2g2000vbb.googlegroups.com...
alariq wrote:
is it correct to use this define
typedef<typeame T>
struct my_helper {
char c;
T data;
};
#define aligned_at(C) ( sizeof(my_hepler<C>) - sizeof(C) )
Neat!
FWIW, the construct above goes not give correct answer when you pass it a
reference. Here is an example:
______________________________________________________________
template<typename T>
struct my_helper {
char c;
T data;
};
#define aligned_at(C) ( sizeof(my_helper<C>) - sizeof(C) )
#include <cstdio>
#define ATTR_ALIGN(V) __attribute__((aligned(V)))
struct my_struct
{
char ATTR_ALIGN(16) buffer;
};
int
main()
{
{
std::printf("my_struct = %lu\n"
"my_struct& = %lu\n",
(unsigned long)aligned_at(my_struct),
(unsigned long)aligned_at(my_struct&));
}
return 0;
}
______________________________________________________________
I am getting the following output:
my_struct = 16
my_struct& = 4294967288
<corrected align hack>
______________________________________________________________
template<typename T>
struct alignof_impl
{
struct holder
{
T m_state;
};
struct aligner
{
char m_pad;
holder m_holder;
};
};
#define ALIGNOF(T) \
(sizeof(alignof_impl<T>::aligner) - \
sizeof(alignof_impl<T>::holder))
#define ATTR_ALIGN(V) __attribute__((aligned(V)))
#include <cstdio>
struct my_struct
{
char ATTR_ALIGN(16) buffer;
};
int
main()
{
{
std::printf("my_struct = %lu\n"
"my_struct& = %lu\n",
(unsigned long)ALIGNOF(my_struct),
(unsigned long)ALIGNOF(my_struct&));
}
return 0;
}
______________________________________________________________
I get:
my_struct = 16
my_struct& = 4
[...]