gpt4 book ai didi

c++ - 放置新+数组+对齐

转载 作者:IT老高 更新时间:2023-10-28 23:12:54 25 4
gpt4 key购买 nike

SomeObj<unsigned int>* Buffer;
char* BufferPtr = MemoryManager::giveMeSomeBytes(resX*resY*sizeof(SomeObj<unsigned int>));
Buffer = new(BufferPtr) SomeObj<unsigned int>[resX*resY];

当我使用调试器跳过这些行时,它会显示变量 Buffer 和 BufferPtr 的值:

BufferPtr: 0x0d7f004c
Buffer: 0x0d7f0050

我真的不明白为什么这些值不同。按照我的理解,placement new 应该使用从地址“BufferPtr”开始的内存来使用分配内存上的默认构造函数初始化数组元素,并返回指向数组中第一个元素的第一个字节的指针,这应该是与传递给placement new 运算符的字节完全相同。

我是不是理解错了,或者有人能告诉我为什么这些值不同吗?

谢谢!

//编辑:好的 - 我进一步调查了这个问题,得到了更令人困惑的结果:

    int size = sizeof(matth_ptr<int>);

char* testPtr1 = (char*)malloc(a_resX*a_resY*sizeof(int));
int* test1 = new(testPtr1) int[a_resX*a_resY];

char* testPtr2 = mmgr::requestMemory(a_resX*a_resY*sizeof(int));
int* test2 = new(testPtr2) int[a_resX*a_resY];

char* testPtr3 = (char*)malloc(a_resX*a_resY*sizeof(matth_ptr<int>));
matth_ptr<int>* test3 = new(testPtr3)matth_ptr<int>[a_resX*a_resY];

char* testPtr4 = mmgr::requestMemory(a_resX*a_resY*sizeof(matth_ptr<int>));
matth_ptr<int>* test4 = new(testPtr4)matth_ptr<int>[a_resX*a_resY];

调试器为我的变量返回以下值:

size: 4

testPtr1:0x05100418
test1: 0x05100418
testPtr2:0x0da80050
test2: 0x0da80050

testPtr3:0x05101458
test3: 0x0510145c
testPtr4:0x0da81050
test4: 0x0da81054

所以它显然必须与我的通用智能指针类 matth_ptr 有关,所以它是:

template <class X> class matth_ptr
{
public:
typedef X element_type;

matth_ptr(){
memoryOfst = 0xFFFFFFFF;
}

matth_ptr(X* p)
{
unsigned char idx = mmgr::getCurrentChunkIdx();
memoryOfst = (int)p-(int)mmgr::getBaseAddress(idx);
assert(memoryOfst<=0x00FFFFFF || p==0);//NULL pointer is not yet handled
chunkIdx = idx;
}
~matth_ptr() {}
X& operator*() {return *((X*)(mmgr::getBaseAddress(chunkIdx)+(memoryOfst&0x00FFFFFF)));}
X* operator->() {return ((X*)(mmgr::getBaseAddress(chunkIdx)+(memoryOfst&0x00FFFFFF)));}
X* get() {return ((X*)(mmgr::getBaseAddress(chunkIdx)+(memoryOfst&0x00FFFFFF)));}


template<typename T>
matth_ptr(const matth_ptr<T>& other) {memoryOfst=other.memoryOfst;}//put these two operators into the private part in order to prevent copying of the smartpointers
template<typename T>
matth_ptr& operator=(const matth_ptr<T>& other) {memoryOfst = other.memoryOfst; return *this;}
template<typename T>
friend class matth_ptr;
private:

union //4GB adressable in chunks of 16 MB
{
struct{
unsigned char padding[3]; //3 bytes padding
unsigned char chunkIdx; //8 bit chunk index
};
unsigned int memoryOfst; //24bit address ofst
};

};

谁能解释一下这是怎么回事?谢谢!

最佳答案

在数组上放置 new 时要小心。在当前的标准中查看第 5.3.4.12 节,您会发现:

new(2,f) T[5] results in a call of operator new[](sizeof(T)*5+y,2,f)

很明显,它将期望放置 new 运算符为其分配超出数组内容所需的额外空间。 “y”仅指定为非负整数值。然后它将以这个量抵消新函数的结果。

还可以查看 18.4.1.3.4,它说放置 new 运算符只是返回提供的指针。这显然是预期的部分。

基于 5.3.4.12,由于每次调用数组的偏移量可能不同,因此标准基本上意味着无法分配所需的确切大小。实际上,该值可能是恒定的,您可以将其添加到分配中,但他的数量可能会因平台而异,并且如标准所述,每次调用都会发生变化。

关于c++ - 放置新+数组+对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4011577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com