What is placement new?

Showing Answers 1 - 5 of 5 Answers

vvvv

  • Sep 16th, 2005
 

The dynamic objects will be created in a pre-allocated heap memory.Accordingly the destructor of each class will call explicitly.

  Was this answer useful?  Yes

Placement new is supported in C++.  As such 'placement new' is an extension to new operator and its usage.   Well, will go through by giving an example.  In embedded systems, memory is very critical (or), any time critical application should not get into memory related problems.

Usually, developers will create memory pools, so that all the memory allocated in the application should be from this memory pool only. 


Ex : char *myMemPool = new char[10000]; // it allocates 10000 bytes of memory.

Now, I am creating one class object, which takes some 500 bytes of data + some functions e t c.

class A *aObject = new (myMemPool) A("I am the data to A"); // this is placement new allocates memory from memory pool .

class A *bObject = new A("I am no special");  // ordinary heap allocation

Cheers.
/Siva

vidhudatta

  • Sep 23rd, 2009
 

Placement syntax allows programmers to explicitly specify memory management of objects. The placement versions of new operator is called placement new.

  Was this answer useful?  Yes

Using placement new operator user can allocate memory in a specific memory address.

Example:
SomeClass at a particular memory address stored in a pointer type variable named pmem
new(pmem) SomeClass;

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions