Placement New

Category: visual studio vclanguage

Question

LokanathNayak on Sun, 06 Apr 2014 03:17:01


Hi,

I want to understand placement New. Please correct me if i am wrong anywhere.

Placement new means, place the object in a specific memory.

Ex.

int arr[10];

A* ptr = new(arr) A(50); 

So here i am placing A at location arr.

Now if i am writing the placement new:

void* operator new(size_t, void* loc)

{
return loc;
}

Here i am not allocating any memory. So my question is :

- is my placement new implementation correct?-

- How the memory will allocated for ptr.

Thanks a lot in advance

Replies

Igor Tandetnik on Sun, 06 Apr 2014 03:39:58


On 4/5/2014 11:17 PM, LokanathNayak wrote:

Placement new means, place the object in a specific memory.

Well, one particular application of placement new syntax means this.

Now if i am writing the placement new:

void* operator new(size_t, void* loc)

You shouldn't. This function is reserved; a program that attempts to replace it exhibits undefined behavior.

So my question is :

- is my placement new implementation correct?

It would have been correct had the program been allowed to replace this function - which it isn't.

- How the memory will allocated for ptr.

The same way memory is allocated for arr. ptr points to the same location that arr points to.


Igor Tandetnik

LokanathNayak on Mon, 07 Apr 2014 03:40:06


Hi Igor,

Could you please let me now the code for placement new, which can allocate memory at a specified location. Because my above code is only specifying the location. Does it mean that i need to overload one more new operator without the location argument.

Thanks

Igor Tandetnik on Mon, 07 Apr 2014 04:03:09


On 4/6/2014 11:40 PM, LokanathNayak wrote:

Could you please let me now the code for placement new, which can allocate memory at a specified location.

What do you mean, allocate memory at the specified location? Where would you obtain a location that doesn't already have memory allocated for it?

What exactly are you trying to achieve, and cannot with standard library-provided functions?

Does it mean that i need to overload one more new operator without the location argument.

What you need to do rather depends on what the purpose of the exercise is. Which you've never adequately explained.

LokanathNayak on Tue, 08 Apr 2014 06:13:25


Hi Igor,

Let me refine the question.

I want to understand the placement new with a sample application.As of my knowldge placement new is allocate the memory at a specific location. So let i need to allocate memory for a class object at the location 0xFFFF. 0xFFFF is the starting address of the block and its available (assume).

So my code is as below, correct me if the syntax is not correct.

A* ptr = new(0xFFFF) A(50); 

Now to validate the above code, i need to overload the placement new. The code i have written as below:

void* operator new(size_t, void* loc)

{
return loc;
}

But here i am returning the location address only, no memory is allocated.

So the summary is, how should i write a placement new overload which will allocate the memory at a specific address.

_______________________________________________________________________

If my entire concept for placement new is incorrect then please explain with a sample piece of code or send some link so that i will have the good understanding of placement new.

Thanks a lot

Mike Danes on Tue, 08 Apr 2014 07:07:06


"0xFFFF is the starting address of the block and its available (assume)."

Nope, 0xffff isn't available. More generally, if a block of memory wasn't allocated using a memory allocation function (malloc for example) then that block of memory isn't available.

"Now to validate the above code, i need to overload the placement new."

No, you don't need to overload the placement new to validate that code. You just need to compile and run the code that uses placement new.

"But here i am returning the location address only, no memory is allocated."

That's the whole point of using placement new, to construct objects in memory that was previously allocated. placement new does not allocate any memory.

"So the summary is, how should i write a placement new overload which will allocate the memory at a specific address."

In general you do not allocate memory at a specific address, memory allocation functions such as malloc return whatever address happens to be available when you call them. It's sort of possible by using certain Win32 functions but it's not guaranteed to work and more importantly this has nothing to do with placement new.

Igor Tandetnik on Tue, 08 Apr 2014 13:04:01


On 4/8/2014 2:13 AM, LokanathNayak wrote:

As of my knowldge placement new is allocate the memory at a specific location.

No. Placement new constructs an object at a specific location, for which memory was previously allocated. The point of exercise is to separate the act of allocating raw memory from the act of running the constructor on that memory (as opposed to regular "new", which combines these two actions into a single call).

So let i need to allocate memory for a class object at the location 0xFFFF.

Why do you believe you need to do that? In any case, you can't.

So the summary is, how should i write a placement new overload which will allocate the memory at a specific address.

You can't just pick up an arbitrary address and say "let there be memory". It don't work that way.