In C++ placement new is used to construct an object at a particular memory location or to pass additional arguments to an allocation function.
Questions tagged [placement-new]
391 questions
0
votes
1 answer
Placement new, objects placed over I/O registers and zeroed memory
I've been experimenting with placement new to "map" classes on top of I/O space to save some memory:
hardware::ioport *port = new(0xWHATEVER) hardware::ioport();
which works nicely, but zeros out the bytes at 0xWHATEVER. The "alternative"…

Farcaller
- 3,070
- 1
- 27
- 42
0
votes
1 answer
Destructor on polymorphic placed stuff
Is there a way to call destructor on objects if they are polymorphic types created with placement new?
class AbstractBase{
public:
~virtual AbstractBase(){}
virtual void doSomething()=0;
};
class Implementation:public virtual…

CoffeDeveloper
- 7,961
- 3
- 35
- 69
0
votes
1 answer
CXX0030 Expression cannot be evaluated
I have a misunderstanding here. I'm designing a Queue class which uses the Client class as the base unit. The list structure consists of a pointer to the next data and a variable for holding the Client object.
The Queue class has 2 ways of…

Robert Lucian Chiriac
- 686
- 2
- 15
- 24
0
votes
1 answer
load_construct_data in boost: problems with placement new
I try to serialize derived pointer class with non-default constructor with the help of boost.
During the compilation I get an error:
Derived.h: In function ‘void boost::serialization::load_construct_data(Archive&, const A::Derived*, unsigned…

Vyacheslav
- 113
- 1
- 9
0
votes
1 answer
Recursive placement allocation
Is it possible to make a recursive placement allocation?
if I have this class:
class A
{
private:
int m_filed1;
char* m_field2;
public:
A(int size)
{
m_field1 = size;
m_field2 = new char[size];
}
};
and I want to dynamically…

Idov
- 5,006
- 17
- 69
- 106
0
votes
2 answers
Creating object pointer independent of constructor argument
I am trying to create a vector like container Vector.
Then declared:
Vector< A> Avector.
While allocating memory it gives compilation error that A does not have a default constructor.
I wrote the following code to allocate memory.
char *pBuffer…

learning
- 94
- 6
0
votes
1 answer
C++: Placement new collides with own new overload
I have overloaded the new operator for a type X to use some memory pool. My new operator takes no user-defined arguments, thus, the only argument is the size of the object of type size_t. At another part of the program, I need placement new for the…

gexicide
- 38,535
- 21
- 92
- 152
0
votes
1 answer
What is the extra cost of overloading placement new operator?
We want to overload placement new operator just to verify that used memory size is enough for the given class. We know this size. The construction is more or less in this way:
template
class PlacementNewTest {
public:
void*…

PiotrNycz
- 23,099
- 7
- 66
- 112
0
votes
2 answers
How do you allocate memory at a predetermined location?
How do i allocate memory using new at a fixed location? My book says to do this:
char *buf=new char[sizeof(sample)];
sample *p=new(buf)sample(10,20);
Here new is allocating memory at buf's address, and (10,20) are values being passed. But what is…

Nirvan
- 147
- 1
- 1
- 6
-1
votes
1 answer
C++ Declare Aligned Storage for Placement-New
I've created some templates in order to create aligned-storage capable of being used for placement-new:
template
constexpr size_t max_alignof()
{
if constexpr (sizeof...(U))
return std::max(alignof(T),…

Jeff G
- 4,470
- 2
- 41
- 76
-1
votes
1 answer
Would using placement new make the following code valid?
I am building a buffer that will be used in a class and wanted to know if the following is valid according to the C++ standard:
#include
#include
int main() {
alignas(std::int32_t) char A[sizeof(std::int32_t)] = { 1, 0, 0, 0…

Adrian
- 10,246
- 4
- 44
- 110
-1
votes
3 answers
Is there a way to assign a stacked object to the allocated memory using placement new?
Here is my program:
#include
using namespace std;
class Object {
public:
Object() { cout << "Object constructor!" << endl; }
~Object() { cout << "Object destructor!" << endl; }
Object(const Object& obj) { information =…

Oleg
- 1,027
- 1
- 8
- 18
-1
votes
1 answer
How to free memory by pointer a (placement new)?
I can't understand how to properly free the memory by pointer a
MemoryManager mm;
char* a = new(mm) char[len];

Vladislav Laptev
- 3
- 1
-1
votes
1 answer
How to expansion a placement new pointer?
Assuming there have a pointer who made by placement new, it's size is 4, how can made it bigger without copy to an other pointer and delete it?
int* ptr = (int*)(::operator new(sizeof(int))); //how to make it bigger than now?

LzxHahaha
- 47
- 9
-1
votes
3 answers
C++: allocation in a fixed block of memory in a loop
I have an application which basically goes like this:
Init();
while(true){
read_data();
process_data();
write_processed_data();
}
the data allocated in a passage of the loop is entirely discarded once we have finished…

user1741137
- 4,949
- 2
- 19
- 28