覆盖特定类的new和delete运算符的原因是什么?(What are the reasons to override new and delete operator for a specific c

系统教程 行业动态 更新时间:2024-06-14 17:01:34
覆盖特定类的new和delete运算符的原因是什么?(What are the reasons to override new and delete operator for a specific class?)

可以为特定类重写动态内存分配运算符 - new和delete。 我可以定义一个不同于Windows上C ++运行时提供的默认内存分配方案。 我几乎没有与之相关的问题:

是否覆盖了特定类可移植的new和delete运算符,以便它也适用于unix? 它是C ++标准的一部分吗?

有哪些情况可能需要您覆盖特定类的内存分配运算符? 只有我能想到的情况是 - 跟踪泄漏的内存分配。

The dynamic memory allocation operators - new and delete can be overridden for a specific class. I could define a different memory allocation scheme than the default that is provided by the C++ Run-time on windows. I have few questions related to it:

Is overriding new and delete operator for a specific class portable such that it also works on unix? Is it part of the C++ standard?

What are the cases that might require you to override memory allocation operators for a specific class? Only case that I could think of is - tracking memory allocations for leaks.

最满意答案

是的,它是可移植的,但要注意MS编译器处理它的“nothrow”部分不同于ANSI标准 - 被烧毁了。

至于为什么,是跟踪内存,跟踪泄漏,跟踪分配都很好,但这些可以全局完成。 您可以为VRAM分配等内容覆盖new / delete,但我通常会在不同的函数中看到这种事情。 覆盖新/删除的最大原因是,如果您更好地了解内存系统的使用方式,并且可以比供应商提供的新的/通用的新/删除版本更有效地定制您的功能。 小对象分配是一种经常被忽视或实现不当的东西,以及为了调试目的而需要分配多少信息。

Yes, it is portable, but beware that MS compilers handle the 'nothrow' part of it different than the ANSI standard - got burned on that.

As to why, yes track memory, tracking leaks, tracking allocations are all nice, but those can be done globally. You could overrride new/delete for something like VRAM allocations, but I usually see that sort of thing called out in different functions. The biggest reason to override new/delete is if you have a better understanding of how your memory system is going to be used, and can tailor your functions more efficiently than the nice, but generic versions of new/delete supplied by your vendor. Small object allocation comes to mind as something that is quite often overlooked or poorly implemented, along with just how much info you want to pass around with allocations for debug purposes.

更多推荐

本文发布于:2023-04-20 18:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9b22af740a6606616ed5e9497ee479f0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   原因   reasons   delete   specific

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!