简体   繁体   中英

Why do both pointers have the same memory address?

#include <iostream>

using namespace std;

int main()
{
    char* MainBlock = new char[100];

    char* SubBlock1 = new (MainBlock) char[20];

    char* SubBlock2 = new (MainBlock) char [20];

    cout  << static_cast<void*>(SubBlock1) << " " << static_cast<void*>(SubBlock2);


}

Why do both the pointers in the code above have the same address? I expected SubBlock2 to be 20 bytes after SubBlock 1.

Does this mean I can allocate an endless number of pointers with placement new even though I only have 100 bytes?

How can I ensure that SubBlock6 will be a nullptr or out of bounds using placement new?

Why do both the pointers in the code above have the same address?

Placement new accepts the exact address where it will initialize the object being created. You pass the same address, you get the same address.

Does this mean I can allocate an endless number of pointers with placement new even though I only have 100 bytes?

No. Each placement new reuses the storage. You can of course reuse the storage infinitely many times, but you will only ever have the same 100 characters allocated at most.

How can I ensure that SubBlock6 will be a nullptr or out of bounds using placement new?

There is no way. The onset is on you to provide valid storage for placement new to create the objects. If you don't, the behavior is undefined.

And finally, you don't need to muck about with placement new.

char *SubBlock1 = MainBlock;
char *SubBlock2 = MainBlock + 20;

Partitions the buffer just fine. Just be sure to delete[] only the pointer value stored in MainBlock .

The (MainBlock) argument is a placement argument. You are in fact explicitly telling the program to allocate both SubBlock1 and SubBlock2 at the address of MainBlock .

Each new expression to get the address at which to construct the object calls the appropriate allocation function . Once the allocation is done and the address is returned from the allocation function, it attempts to construct the object exactly at the specified address.

Given char* SubBlock1 = new (MainBlock) char[20]; , it calls the following allocation function:

void* operator new[]( std::size_t count, void* ptr );

Called by the standard array form placement new expression. The standard library implementation performs no action and returns ptr unmodified.

As the documentation above say, calling this allocation function does nothing and returns the address you passed unmodified. So, this new expression constructs 20 char exactly at MainBlock . This is why you get the same address for both SubBlock1 and SubBlock2 .


Does this mean I can allocate an endless number of pointers with placement new even though I only have 100 bytes?

No. Note that allocation and construction are two different things. In your example, you allocate the memory only once and construct objects many times on it. So the layout of objects constructed on the allocated memory is up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM