I'm trying to understand the rules for alignment with placement new.
If we have
struct A
{
size_t length;
};
my understanding (please correct me if I'm wrong) is that this results
in a
data structure that is properly aligned:
typedef typename std::aligned_storage<sizeof(A),
alignof(A)>::type storage_type;
char* storage = new char [sizeof(storage_type)];
A* pa = new(storage)A();
But what if we have
struct B
{
size_t length;
int *p;
};
and wish to allocate storage for B and p with placement new from the
same storage?
Does this satisfy alignment rules?
typedef typename std::aligned_storage<sizeof(B),
alignof(B)>::type storage_type;
size_t length = 10;
char* storage = new char
[sizeof(storage_type)+length*sizeof(int)];
B* pb = new(storage)B();
pb->p = new(storage + sizeof(storage_type))int[length];
On Sunday, 13 December 2015 19:10:14 UTC+1, Daniel wrote:
But what if we have
struct B
{
size_t length;
int *p;
};
and wish to allocate storage for B and p with placement new from the
same storage?
I think you need to define:
struct BB { struct B b; int i[1] };
typedef typename std::aigned_storage<sizeof(B),
alignof(B)>::type storage_type;
size_t length = 10;
char* storage = new char[sizeof(storage_type)+(length-1)*sizeof(int)];
B* pb = new(storage)B();
auto pbb = reinterpret_cast<BB*>(storage);
pb->p = new(&pbb->i)int[length];
On Monday, December 14, 2015 at 8:00:20 AM UTC-5, Martin Bonner wrote:
On Sunday, 13 December 2015 19:10:14 UTC+1, Daniel wrote:
But what if we have
struct B
{
size_t length;
int *p;
};
and wish to allocate storage for B and p with placement new from the
same storage?
I think you need to define:
struct BB { struct B b; int i[1] };
typedef typename std::aigned_storage<sizeof(B),
alignof(B)>::type storage_type;
size_t length = 10;
char* storage = new char[sizeof(storage_type) +
(length-1)*sizeof(int)];
B* pb = new(storage)B();
auto pbb = reinterpret_cast<BB*>(storage);
pb->p = new(&pbb->i)int[length];
Thank you very much! that definitely helps my understanding. But did you
mean to write BB instead of B in the typedef for storage_type?
I'm trying to understand the rules for alignment with placement new.
If we have
struct A
{
size_t length;
};
my understanding (please correct me if I'm wrong) is that this results
in a
data structure that is properly aligned:
typedef typename std::aligned_storage<sizeof(A),
alignof(A)>::type storage_type;
char* storage = new char [sizeof(storage_type)];
A* pa = new(storage)A();
But what if we have
struct B
{
size_t length;
int *p;
};
and wish to allocate storage for B and p with placement new from the
same storage?
On Sunday, December 13, 2015 at 12:10:14 PM UTC-6, Daniel wrote:[snip]
I'm trying to understand the rules for alignment with placement new.
If we have
struct A
{
size_t length;
};
my understanding (please correct me if I'm wrong) is that this results
in a
data structure that is properly aligned:
typedef typename std::aligned_storage<sizeof(A),
alignof(A)>::type storage_type;
char* storage = new char [sizeof(storage_type)];
A* pa = new(storage)A();
But what if we have
struct B
{
size_t length;
int *p;
};
and wish to allocate storage for B and p with placement new from the
same storage?
Wouldn't using std::aligned_union:
http://www.cplusplus.com/reference/type_traits/aligned_union/
be simpler? IOW:
using storage_type = aligned_union<Len,A,B>::type;
The only thing that needs to be calculated would be the Len
template parameter, which would just be the max of
the sizeof B and A.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 292 |
Nodes: | 16 (2 / 14) |
Uptime: | 206:18:41 |
Calls: | 6,618 |
Calls today: | 2 |
Files: | 12,168 |
Messages: | 5,316,882 |