简体   繁体   English

为什么两个指针都有相同的内存地址?

[英]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. 我希望SubBlock2在SubBlock 1之后是20个字节。

Does this mean I can allocate an endless number of pointers with placement new even though I only have 100 bytes? 这是否意味着即使我只有100个字节,我可以分配无限数量的指针与新的位置?

How can I ensure that SubBlock6 will be a nullptr or out of bounds using placement new? 如何使用placement new确保SubBlock6是nullptr还是越界?

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. Placement new接受初始化正在创建的对象的确切地址。 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? 这是否意味着即使我只有100个字节,我可以分配无限数量的指针与新的位置?

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. 当然,您可以无限次地重复使用存储,但您最多只能分配相同的100个字符。

How can I ensure that SubBlock6 will be a nullptr or out of bounds using placement new? 如何使用placement new确保SubBlock6是nullptr还是越界?

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 . 只需确保仅delete[]存储在MainBlock的指针值。

The (MainBlock) argument is a placement argument. (MainBlock)参数是一个放置参数。 You are in fact explicitly telling the program to allocate both SubBlock1 and SubBlock2 at the address of MainBlock . 实际上,您明确告诉程序在SubBlock2的地址分配SubBlock1MainBlock

Each new expression to get the address at which to construct the object calls the appropriate allocation function . 获取构造对象的地址的每个new表达式都调用适当的分配函数 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]; 给定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. 标准库实现不执行任何操作并返回未修改的ptr。

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 . 因此,这个新表达式在MainBlock构造了20个char This is why you get the same address for both SubBlock1 and SubBlock2 . 这就是为什么你得到SubBlock1SubBlock2相同的地址。


Does this mean I can allocate an endless number of pointers with placement new even though I only have 100 bytes? 这是否意味着即使我只有100个字节,我可以分配无限数量的指针与新的位置?

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. 因此,在分配的内存上构建的对象的布局由您决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当两个指针都位于同一内存时,为什么必须两次调用“删除”? - Why do I have to call “delete” twice when I have each two pointers at same memory? 为什么指针的地址与它们指向的变量的地址不同? - Why don't pointers have same address as the variable they are pointing to? 为什么变量指针包含相同数据类型的地址? - Why do variable pointers contain the address of the same data type? 为什么两个函数具有相同的地址? - Why do two functions have the same address? 为什么函数指针都具有相同的值? - Why do function pointers all have the same value? 指向结构的指针向量的元素具有相同的地址 - Elements of a Vector of pointers to structs have same address 为什么vector的随机访问迭代器给出与指针不同的相同memory地址? - Why does vector's random access iterator gives the same memory address unlike pointers? 为什么rvalue / const引用有内存地址和大小? - Why do rvalue/const references have memory address and size? C ++为什么它不是相同的地址(指针) - C++ Why it's not the same address (pointers) 数组中的相同字符串具有相同的内存地址 - Same strings in array have same memory address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM