重写“new”操作符有什么有用的功能吗?

时间:2022-01-12 17:06:47

What new kind of functionalities (for debugging or not) do you find helpful by overriding the new operator?

通过覆盖新的操作符,您觉得哪种新功能(不管是否用于调试)有用?

5 个解决方案

#1


7  

The main reason I've had to overload new has been for performance. One example is allocating a large number of small objects, which is often fairly slow with a general purpose allocator, but can often be improved a lot with a custom allocator.

我不得不重载new的主要原因是性能。一个例子是分配大量的小对象,对于通用的分配器来说,这通常是相当慢的,但是使用自定义分配器通常可以得到很大的改进。

#2


4  

There are many cases when overriding operator new must be done. As someone said, it's mainly for custom memory management.

在许多情况下,重写操作符new必须完成。正如有人所说,它主要用于自定义内存管理。

The main example that I've personally dealt with was for embedded systems.

我个人处理过的主要例子是嵌入式系统。

In an embedded system, dynamic memory allocation is usually a problem. Even if you have an embedded operating system that supports it, you have all sorts of issues you might have to deal with (for example, embedded systems often need to be more deterministic than their PC counterparts. Also, you might be working on a system that is supposed to be up for years at a time, in which case memory fragmentation is a real problem).

在嵌入式系统中,动态内存分配通常是一个问题。即使你有一个支持它的嵌入式操作系统,你也可能会遇到各种各样的问题(例如,嵌入式系统通常需要比PC系统更具有确定性。此外,您可能正在开发一个系统,该系统应该在一段时间内持续更新,在这种情况下,内存碎片是一个真正的问题。

Because of this, embedded systems tend to disallow dynamic memory allocation completely.

因此,嵌入式系统倾向于完全不允许动态内存分配。

Of course, in most software projects you need some form of dynamic memory, and in those cases you generally implement your own (i.e., have some kind of memory pool that all the threads can ask for memory from, etc).

当然,在大多数软件项目中,您需要某种形式的动态内存,在这些情况下,您通常实现您自己的内存(即。,拥有某种内存池,所有线程都可以向其请求内存,等等)。

To make C++ projects use this hand-rolled dynamic memory allocation in the easiest way, I've seen projects override operator new to use the project's allocation method.

要使c++项目以最简单的方式使用这种手动滚动的动态内存分配,我看到过使用项目分配方法的项目覆盖操作符。

#3


3  

Well, custom memory management and debugging. That's about it, and all you can get out of it too.

好的,自定义内存管理和调试。就是这样,你能从中得到的也只有这些。

#4


2  

You might be developing a library that requires some extra stage in initializing certain objects, overloading allows you to do that without some clunky extra calls that the user would otherwise end up having to type.

您可能正在开发一个在初始化某些对象时需要额外阶段的库,重载允许您这样做,而不需要用户在其他情况下不得不输入一些笨重的额外调用。

#5


2  

If you're working with SSE / SIMD, you generally need your memory to be 4-word-aligned, so you might overload new for the relevant classes to use memalign() (whereas the default new is doing something equivalent to plain malloc()).

如果您正在使用SSE / SIMD,那么通常需要您的内存是4字对齐的,因此您可能会重载相关类以使用memalign()(而默认的new是执行与plain malloc()相同的操作)。

#1


7  

The main reason I've had to overload new has been for performance. One example is allocating a large number of small objects, which is often fairly slow with a general purpose allocator, but can often be improved a lot with a custom allocator.

我不得不重载new的主要原因是性能。一个例子是分配大量的小对象,对于通用的分配器来说,这通常是相当慢的,但是使用自定义分配器通常可以得到很大的改进。

#2


4  

There are many cases when overriding operator new must be done. As someone said, it's mainly for custom memory management.

在许多情况下,重写操作符new必须完成。正如有人所说,它主要用于自定义内存管理。

The main example that I've personally dealt with was for embedded systems.

我个人处理过的主要例子是嵌入式系统。

In an embedded system, dynamic memory allocation is usually a problem. Even if you have an embedded operating system that supports it, you have all sorts of issues you might have to deal with (for example, embedded systems often need to be more deterministic than their PC counterparts. Also, you might be working on a system that is supposed to be up for years at a time, in which case memory fragmentation is a real problem).

在嵌入式系统中,动态内存分配通常是一个问题。即使你有一个支持它的嵌入式操作系统,你也可能会遇到各种各样的问题(例如,嵌入式系统通常需要比PC系统更具有确定性。此外,您可能正在开发一个系统,该系统应该在一段时间内持续更新,在这种情况下,内存碎片是一个真正的问题。

Because of this, embedded systems tend to disallow dynamic memory allocation completely.

因此,嵌入式系统倾向于完全不允许动态内存分配。

Of course, in most software projects you need some form of dynamic memory, and in those cases you generally implement your own (i.e., have some kind of memory pool that all the threads can ask for memory from, etc).

当然,在大多数软件项目中,您需要某种形式的动态内存,在这些情况下,您通常实现您自己的内存(即。,拥有某种内存池,所有线程都可以向其请求内存,等等)。

To make C++ projects use this hand-rolled dynamic memory allocation in the easiest way, I've seen projects override operator new to use the project's allocation method.

要使c++项目以最简单的方式使用这种手动滚动的动态内存分配,我看到过使用项目分配方法的项目覆盖操作符。

#3


3  

Well, custom memory management and debugging. That's about it, and all you can get out of it too.

好的,自定义内存管理和调试。就是这样,你能从中得到的也只有这些。

#4


2  

You might be developing a library that requires some extra stage in initializing certain objects, overloading allows you to do that without some clunky extra calls that the user would otherwise end up having to type.

您可能正在开发一个在初始化某些对象时需要额外阶段的库,重载允许您这样做,而不需要用户在其他情况下不得不输入一些笨重的额外调用。

#5


2  

If you're working with SSE / SIMD, you generally need your memory to be 4-word-aligned, so you might overload new for the relevant classes to use memalign() (whereas the default new is doing something equivalent to plain malloc()).

如果您正在使用SSE / SIMD,那么通常需要您的内存是4字对齐的,因此您可能会重载相关类以使用memalign()(而默认的new是执行与plain malloc()相同的操作)。