一起使用 smart_pointer 和 set_new_handler

发布于 2024-11-29 16:38:10 字数 78 浏览 3 评论 0原文

是否需要担心将 set_new_handlers 与智能或自动指针结合使用,或者库中包含具有精致错误处理功能的处理程序?

谢谢

Do one need to worry about using set_new_handlers in conjunction with smart or auto pointer's or the libraries come packed with handlers with refined error handling?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉南桥 2024-12-06 16:38:10

C++ 附带的智能指针(目前只有 auto_ptr,但很快包括 shared_ptrweak_ptr)都使用 new默认情况下,因此它们将自动与全局新处理程序交互。您不需要做任何特殊的事情来确保在使用这些类时调用新的处理程序。

希望这有帮助!

The smart pointers that come with C++ (currently just auto_ptr, but soon including shared_ptr and weak_ptr) all use new by default, so they'll interface with the global new handler automatically. You don't need to do anything special to ensure that the new handler is invoked when using these classes.

Hope this helps!

没︽人懂的悲伤 2024-12-06 16:38:10

我认为你正在担心一些不相关的事情。 new 处理程序是一个细节,指定operator new 在找不到足够的内存来分配的情况下如何操作。智能指针是库提供的容器类。大多数 C++ 标准库容器需要分配内存,它们通过分配器类以模块化方式进行分配。默认分配器可能(并且通常确实)使用 new 来获取内存,但是内存分配和对象构造的过程是解耦的,并且任何库类中都不需要知道如何分配内存的任何细节。 new 运算符有效,反之亦然。

因此,无论您对 new 运算符做什么(例如通过安装处理程序),都将透明地适用于标准库构造,并且任何一方都无需了解另一方。

I think you're worrying about unrelated things. The new handler is a detail that specifies how operator new operates in the event that it cannot find enough memory to allocate. A smart pointer is a library-provided container class. Most C++ standard library containers need to allocate memory, which they do in a modular fashion by means of an allocator class. The default allocator may (and usually does) use new to obtain memory, but the process of memory allocation and object construction is decoupled, and there's nothing in any library class that needs to know any details of how the new operator works, and vice versa.

So whatever you do to your new operator (e.g. by installing handlers) will transparently work for standard library constructions, and there is nothing either side has to know about the other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文