Groups | Search | Server Info | Login | Register


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

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

Message-ID <4e7be5e0-6530-4995-97f0-856ae929cfc6@googlegroups.com> (permalink)
Newsgroups comp.lang.c++.moderated
From Daniel <danielaparker@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> <82ccb967-4264-4420-b627-533a10e8ada8@googlegroups.com>
Date 2015-12-15 07:08 -0600

Show all headers | View raw


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?

Best regards,
Daniel


-- 
      [ 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 | Next 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