Groups | Search | Server Info | Login | Register


Groups > comp.lang.c++.moderated > #7368

Re: Alignment, placement new and struct with pointer to array of POD

Message-ID <d41589e3-efe9-4764-b317-39c0dc79bfda@googlegroups.com> (permalink)
Newsgroups comp.lang.c++.moderated
From evansl <cppljevans@googlemail.com>
Subject Re: Alignment, placement new and struct with pointer to array of POD
Organization unknown
References <13729986-bf6d-40c4-8721-0e607a43d766@googlegroups.com> <a9ffdb39-9f0d-45aa-864f-771fa6122458@googlegroups.com>
Date 2016-01-12 07:21 -0600

Show all headers | View raw


On Monday, January 11, 2016 at 6:50:17 AM UTC-6, evansl wrote:
> On Sunday, December 13, 2015 at 12:10:14 PM UTC-6, Daniel wrote:
> > 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.
[snip]
OOPS.  The Len parameter to aligned_union must be a constant,
and it's not since it depends on length, which is a runtime
value.

Sorry for noise :(


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

Back to comp.lang.c++.moderated | Previous | NextPrevious in thread | Find similar


Thread

Alignment, placement new and struct with pointer to array of POD Daniel <danielaparker@googlemail.com> - 2015-12-13 12:01 -0600
  Re: Alignment, placement new and struct with pointer to array of POD Martin Bonner <martinfrompi@yahoo.co.uk> - 2015-12-14 06:52 -0600
    Re: Alignment, placement new and struct with pointer to array of POD Daniel <danielaparker@googlemail.com> - 2015-12-15 07:08 -0600
      Re: Alignment, placement new and struct with pointer to array of POD Martin Bonner <martinfrompi@yahoo.co.uk> - 2015-12-15 13:55 -0600
  Re: Alignment, placement new and struct with pointer to array of POD evansl <cppljevans@googlemail.com> - 2016-01-11 06:46 -0600
    Re: Alignment, placement new and struct with pointer to array of POD evansl <cppljevans@googlemail.com> - 2016-01-12 07:21 -0600

csiph-web