实现重载operator new [] / delete [] [英] Implementing overloaded operator new[]/delete[]

查看:76
本文介绍了实现重载operator new [] / delete []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我已经实现了重载operator new并删除了很多

down。只需要满足运算符重载的类的对齐要求



但是如何实现operator new [] / delete []我无法看到

表示删除[],必须销毁多少个对象(或者空间有多大?)或者我可以要弄清楚什么是对齐

的要求,以便在调用我的新[]后实现,可以

填写自己的大小记录。


或者是将指针映射到

size本身所需的C ++实现 - 我真的希望不要这样做。


-

Tristan Wibberley


任何表达的意见都是我的(或者我正在扮演恶魔的主张

的缘故一个好的论点)。我的雇主与这个

通讯无关。

解决方案

9月23日10:30 am,Tristan Wibberley< maihem -... @ maihem.orgwrote:





我有实现重载运算符new和删除几乎

down。只需要满足运算符过载的类的对齐要求




如此简单:编译器计算确切的最小正确大小

可以包含具有正确对齐的对象并将其传递给operator

''new''然后将返回值四舍五入到第一个对齐的对象。


但是如何实现operator new [] / delete [ ]我无法看到

表示删除[],必须销毁多少个对象(或者空间有多大的空间) - 或者我无法弄清楚什么是对齐

的要求,以便在调用我的新[]后,实现可以

填写自己的大小记录。


或者是将指针映射到

size本身所需的C ++实现 - 我真的希望不是:)



new分配的内存比阵列所需的内存多 - 在某些

平台上 - 存储元素的数量。所以

当''删除[]''时,析构函数被调用正确的次数。但是如果

你向上转换数组指针你将面临未定义的行为

可以修复为''delete []''的情况通过引入基础

析构函数作为虚拟,允许编译器获得有关已删除对象大小的运行时信息

。 />


任何表达的意见都是我的(或者我为了一个好的争论而扮演恶魔为b / b $ b b辩护)。我的雇主与这个

通讯无关。



问候,

FM。


但是怎么做一个实现运算符new [] / delete []我看不到
的方法


表示,在删除[]时,必须销毁多少个对象(或有多大

空间是这样的) - 或者我无法弄清楚什么是对齐

的要求,以便在调用我的新[]之后实现

填写自己的大小记录。



我用来跟踪内部表中的基地址/大小对

可由两个运营商访问。


无论如何,我理解的是你想要删除[]来遍历

数组来删除它包含的objets / references ...恕我直言这不是一个好的/>
想法,因为你无法断言数组的一个条目是有效的。


9月23日晚上7:30,Tristan Wibberley< maihem- ... @ maihem.orgwrote:


我已经实现了重载operator new并删除了很多

down。只需要满足运算符过载的类的对齐要求



但是如何实现operator new [] / delete []我怎么也看不到

的方法表示,删除[],必须销毁多少个对象(或者空间有多大?)或者我不知道对齐是什么

要求这样实现,在调用我的新[]后,可以

填写自己的大小记录。



我不确定我理解。通常实现

new [] / delete []只是:


void *

operator new [](size_t n)

{

返回运算符new(n);

}


void

operator delete [](void * p)

{

operator delete(p);

}


操作员删除功能不需要知道涉及多少

对象。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34


Hi

I''ve got implementing overloaded operator new and delete pretty much
down. Just got to meet the alignment requirements of the class on which
the operator is overloaded.

But how does one implement operator new[]/delete[] I can''t see a way to
indicate, on delete[], how many objects must be destroyed (or how big
the space is) - alternatively I can''t figure out what are the alignment
requirements so that the implementation, after calling my new[], can
fill in its own size record.

Or is the C++ implementation required to keep a mapping of pointers to
sizes itself - I really hope not :)

--
Tristan Wibberley

Any opinion expressed is mine (or else I''m playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.

解决方案

On Sep 23, 10:30 am, Tristan Wibberley <maihem-...@maihem.orgwrote:

Hi

I''ve got implementing overloaded operator new and delete pretty much
down. Just got to meet the alignment requirements of the class on which
the operator is overloaded.

so simple : the compiler calculates the exact minimum proper size that
can contain an object with correct alignment and passes it to operator
''new'' then the return is rounded up to first aligned object.

But how does one implement operator new[]/delete[] I can''t see a way to
indicate, on delete[], how many objects must be destroyed (or how big
the space is) - alternatively I can''t figure out what are the alignment
requirements so that the implementation, after calling my new[], can
fill in its own size record.

Or is the C++ implementation required to keep a mapping of pointers to
sizes itself - I really hope not :)

new allocates more memory than needed for the array and - on some
platforms - stores the number of elements at the beginning.so that
destructor is called in correct number of times when ''delete[]''.but if
you upcast the array pointer you will face undefined behavior which
can be fixed for the case of ''delete[]'' via introducing the base
destructor as virtual which allows the compiler to have runtime info
about the size of deleted objects.

Any opinion expressed is mine (or else I''m playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.

regards,
FM.


But how does one implement operator new[]/delete[] I can''t see a way to

indicate, on delete[], how many objects must be destroyed (or how big
the space is) - alternatively I can''t figure out what are the alignment
requirements so that the implementation, after calling my new[], can
fill in its own size record.

I used to keep track of the base address/size pairs in an internal table
accessible by both operators.

Anyway, what I understand is that you want delete[] to walk through the
array to delete objets it contains/references... IMHO this is not a good
idea since you cannot assert that an entry of the array is valid.


On Sep 23, 7:30 pm, Tristan Wibberley <maihem-...@maihem.orgwrote:

I''ve got implementing overloaded operator new and delete pretty much
down. Just got to meet the alignment requirements of the class on which
the operator is overloaded.

But how does one implement operator new[]/delete[] I can''t see a way to
indicate, on delete[], how many objects must be destroyed (or how big
the space is) - alternatively I can''t figure out what are the alignment
requirements so that the implementation, after calling my new[], can
fill in its own size record.

I''m not sure I understand. The usual implementation of
new[]/delete[] is just:

void*
operator new[]( size_t n )
{
return operator new( n ) ;
}

void
operator delete[]( void* p )
{
operator delete( p ) ;
}

The operator delete function doesn''t need to know how many
objects are involved.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于实现重载operator new [] / delete []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆