Skip to content

add overload of ::new for placement support #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

cfmkla
Copy link

@cfmkla cfmkla commented Aug 20, 2019

This adds an overload of ::new that matches the signature required for placement new. For additional background, here's what cppreference has to say about placement new.

When a programmer calls new Foo the existing overload of new is called which uses malloc to provide the required amount of memory. To use placement, the programmer calls new(ptr) Foo where ptr is a correctly aligned pointer to sufficient unused memory to hold an instance of Foo. The overload added in this PR is called which returns the passed in pointer to be used as the address of the newly created object.

To destroy an object allocated with placement new call its destructor explicitly: fooPtr->~Foo(). There is no corresponding "placement delete".

The following is a trivial example of placement new in action:

void *ptr = malloc(sizeof(Foo))
Foo *fooPtr = new(ptr) Foo;
fooPtr->~Foo();
free(ptr);

@aentinger
Copy link
Contributor

Placement new was already proposed in #33 and has just been merged.

@aentinger aentinger closed this Sep 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants