Re: placement new and alignment

From: PaulOBear (PaulOBear_at_discussions.microsoft.com)
Date: 09/15/04


Date: Wed, 15 Sep 2004 08:57:18 -0700

Ok, nevermind... Sigh... Do I feel sheepish. :) According to Deep C++, the
address passed in gets magically assigned to the this pointer in the
constructor of the class. Duh.

And if I just include <new>, it's all done for me already!! :^{=}

Thanks,

Paul

"PaulOBear" wrote:

> Hi Igor and Mycroft -
>
> I am having a related problem, and so perhaps this post should be in another
> thread. However, since you two seem to be working in the same muck as I am,
> I thought you might be able to help. :)
>
> The problem:
>
> I am an embedded systems developer on a PowerPC target, and I've been tasked
> with developing the UI on a Windows platform, since we don't have hardware
> (gee, *that's* never happened before! ;). When developing C++ apps in an
> embedded environment, it is often undesirable to use ordinary new and the
> heap. So we like to use placement new. In my experience, this is most
> typically performed by allocating space in a class to contain the object of
> interest:
>
> byte objectBuf[sizeof(SomeClass)];
>
> Then one merely placement new's the class into the buffer:
>
> SomeClass* pSomeClass = reinterpret_cast<SomeClass*>(new (&objectBuf)
> SomeClass());
>
> All the embedded compilers (that I have used) have such an operator new
> defined as part of the compiler. However, now I am trying to use VC++, and
> lo and behold, it's something that I need to provide.
>
> I presume that if I've allocated the space in the context of the code, then
> it will be aligned properly. Is that a correct assumption?
>
> I have read the documentation on placement new which comes with VC++ and
> tells one that one must define their own placement new operator, but it
> doesn't really go into the details of such a transformation (beyond having to
> register it).
>
> I imagine that I would need a declaration something like:
>
> ::operator new(size_t size, void* loc);
>
> And of course, the definition to match, and that's where I really am lost.
> Ok, so now I have a location to instantiate an object, but how do I do that
> w/o invoking a normal new and w/o invoking another placement new (recursively
> until the stack overwrites some vital organs. ;) .
>
> Thanks in Advance,
>
> Paul Ourada
>
> "Igor Tandetnik" wrote:
>
> > "Mycroft Holmes" <m.holmes@nospam.it> wrote in message
> > news:uF6rslEjEHA.2052@TK2MSFTNGP15.phx.gbl
> > >> Again, remember that the standard requires operator new to produce
> > >> storage suitably aligned for any type, not just some type. Thus,
> > >> calculating an alignment of a single type is not particularly useful.
> > >
> > > thsnks for the link, I'm going to read it immediately.
> > > Anyway I want to compute alignment for type T on demand: my program
> > > executes (well... I'm simplifying... ;)
> > >
> > > void* p = cache.gimmeSomeMemory( instance_of<T>() );
> > > return new(p) T;
> >
> > My (admittedly limited) understanding is that this is formally illegal -
> > though likely to work in all practical situations - unless
> > gimmeSomeMemory() returns memory aligned so as to satisfy requirements
> > on operator new (that is, properly aligned for any type). See
> >
> > http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#114
> >
> > The standard does not explicitly say how the pointer passed to placement
> > new should be aligned, but it says that 1) placement new simply returns
> > the pointer passed to it and 2) all allocation functions (of which
> > placement new is one) must return a pointer suitably aligned for any
> > type. This seems to make it responsibility of the caller to ensure that
> > only a suitably aligned pointer is ever passed to placement new.
> >
> > > so type T is a single type and statically known and I need to ensure
> > > that gimmeSomeMemory returns a good pointer.
> >
> > Again, formally this does not seem to be sufficient, practically it most
> > likely is.
> > --
> > With best wishes,
> > Igor Tandetnik
> >
> > "For every complex problem, there is a solution that is simple, neat,
> > and wrong." H.L. Mencken
> >
> >
> >



Relevant Pages

  • Re: placement new and alignment
    ... The standard does not explicitly say how the pointer passed to placement ... > that gimmeSomeMemory returns a good pointer. ...
    (microsoft.public.vc.language)
  • Re: placement new and alignment
    ... So we like to use placement new. ... > though likely to work in all practical situations - unless ... > The standard does not explicitly say how the pointer passed to placement ... >> that gimmeSomeMemory returns a good pointer. ...
    (microsoft.public.vc.language)
  • Re: delete[] of new char[] fails with debug assertion BLOCK_TYPE
    ... >>the assembly code) checks that the memory being deleted is on the debug heap. ... > evaluates to a pointer, and hence warns you about the scalar delete of an ... > It simply doesn't matter if the compiler warns or not about things which are ... >>for the placement delete such as I have described above. ...
    (microsoft.public.vc.language)
  • Re: delete[] of new char[] fails with debug assertion BLOCK_TYPE
    ... >> are thrown by ctors during placement new. ... >expression; conversion to pointer supplied ... It simply doesn't matter if the compiler warns or not about things which are ... Placement delete is only for dealing with exceptions thrown by ctors during ...
    (microsoft.public.vc.language)
  • Re: What evil things!
    ... Hey, I was not impressed with the things, Paul was trying to squish the bug ... Last night Pointer was tacked ... forwrd going horse but he so looked after Paul. ... Both Paul and I were trying to whack the wasp come something ...
    (uk.rec.equestrian)

Loading