Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
new expression - cppreference.com[^]

Useless.

Placement new operator in C++ - GeeksforGeeks[^]

Bunk.

Placement syntax - Wikipedia[^]

Written by somebody who doesn't care if it's understood by anyone.

Apparently I can't read code.
Apparently I can't copy code.

Or placement new simply does not work.

Can anyone make the code in what have you tried compile?

What I have tried:

C++
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
struct test final {
    test() {
        printf("test::test()\r\n");
    }
    ~test() {
        printf("test::~test()\r\n");
    }
};
template <typename T>
T* create(void* buf) {
    
    T* tptr = new (buf) T;  // Construct a `T` object, placing it directly into your
                            // pre-allocated storage at memory address `buf`
    return tptr;
}
int main(int argc, char** argv) {
    uint8_t ptest_buf[sizeof(test)];
    test* ptest = create<test>(ptest_buf);
    return 0;
}
Posted
Comments
Maxim Kartavenkov 9-Nov-23 5:13am    
Compiled correctly on MSVC

You forgot to include the new header which provides standard library implementations of the placement new (as an alternative, you could provide your own implementations).
The following code compiles and runs fine on my Linux box.
C++
#include <cstdint>
#include <cstdlib>
#include <new>

struct test final {
    test() {
        printf("test::test()\r\n");
    }
    ~test() {
        printf("test::~test()\r\n");
    }
};




template <typename T>
T* create(void* buf) {

    T* tptr = new (buf) T;  // Construct a `T` object, placing it directly into your

                            // pre-allocated storage at memory address `buf`
    return tptr;
}



int main(int argc, char** argv)
{
    uint8_t ptest_buf[sizeof(test)];
    test* ptest = create<test>(ptest_buf);
    printf("%p\n", ptest);
}
 
Share this answer
 
v3
Comments
Maxim Kartavenkov 9-Nov-23 5:16am    
5.
CPallini 9-Nov-23 5:21am    
Thank you.
honey the codewitch 9-Nov-23 8:53am    
+5 thanks. Wow, here I thought it was just a variant of the keyword new. I guess it's a function? Odd.
CPallini 9-Nov-23 9:45am    
You're welcome.
I discovered that while trying to answer your question (before, I just assumed you have to provide your own implementation).
Your call to operator new is not quite correct. It goes something like:
//
//	new(a) type; calls operator new(sizeof(type), a);
//	new(a,b) type[42]; calls operator new(sizeof(type)*42, a, b);
//		thus
//	new(T) calls operator new(sizeof(T));
//

So your template should be something like:
C++
template <typename T>
T* create(void* buf) {
    
    T* tptr = new (T);  // Construct a new `T` struct

    return tptr;
}

// ...

    uint8_t ptest_buf[sizeof(test)]; // You already allocate space for the test struct
    test* ptest = create<test>(ptest_buf); // so what are you trying to do here?

But I am not sure what the buf parameter is for, since you return the pointer to the newly allocated structure.
 
Share this answer
 
v2
Comments
honey the codewitch 9-Nov-23 4:36am    
This is the standard new operator. Not the emplace new operator. I am trying to call new on an existing buffer using the emplace new operator.
Richard MacCutchan 9-Nov-23 4:45am    
Sorry, never heard of that, obviously something I need to study.
CPallini 9-Nov-23 5:22am    
It is a weird thing used by us weird people.
Richard MacCutchan 9-Nov-23 5:34am    
So true.
honey the codewitch 9-Nov-23 8:55am    
In IoT and embedded you have different physical heaps with different sizes and performance characteristics. You have PSRAM and SRAM for example. Because of this, a user of my lib can choose the allocators to use when it needs to dynamically allocate RAM. Right now I've been very careful about RAII on anything that is the target of those allocations, but I'd rather use placement new and fire the constructors cleanly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900