用于堆上内存分配的新运算符

发布于 2024-10-16 16:46:03 字数 330 浏览 7 评论 0原文

我正在看新操作员的签名。即:

void* operator new (std::size_t size) throw (std::bad_alloc);

但是当我们使用这个运算符时,我们从不使用强制转换。即

 int *arr = new int;

,在这种情况下,C++ 如何将 void* 类型的指针转​​换为 int* 。因为,即使 malloc 返回一个 void* 并且我们需要显式使用强制转换。

I was looking at the signature of new operator. Which is:

void* operator new (std::size_t size) throw (std::bad_alloc);

But when we use this operator, we never use a cast. i.e

 int *arr = new int;

So, how does C++ convert a pointer of type void* to int* in this case. Because, even malloc returns a void* and we need to explicitly use a cast.

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

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

发布评论

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

评论(2

污味仙女 2024-10-23 16:46:04

在 C++ 中,operator newnew 运算符之间存在非常微妙的差异。 (再读一遍……顺序很重要!)

operator new 函数是 C++ malloc 函数的 C++ 模拟。它是一个原始内存分配器,其职责只是生成一个用于构造对象的内存块。它不调用任何构造函数,因为那不是它的工作。通常,您不会在 C++ 代码中看到直接使用operator new;看起来有点奇怪。例如:

void* memory = operator new(137); // Allocate at least 137 bytes

new 运算符是一个关键字,负责为对象分配内存并调用其构造函数。这是 C++ 代码中最常遇到的情况。当您编写时,

int* myInt = new int;

您正在使用 new 运算符来分配新整数。在内部,new 运算符的工作原理大致如下:

  1. 使用operator new 分配内存来保存所请求的对象。
  2. 调用对象构造函数(如果有)。如果抛出异常,请使用operator delete释放上述内存,然后传播异常。
  3. 返回指向新构造的对象的指针。

由于 new 运算符和 operator new 是分开的,因此可以使用 new 关键字来构造对象,而无需实际分配任何内存。例如,著名的placement new允许您在用户提供的内存中的任意内存地址构建对象。例如:

T* memory = (T*) malloc(sizeof(T)); // Allocate a raw buffer
new (memory) T(); // Construct a new T in the buffer pointed at by 'memory.'

通过定义自定义 operator new 函数来重载 new 运算符,可以让您以这种方式使用 new;您指定如何进行分配,C++ 编译器会将其连接到 new 运算符中。

如果您好奇的话,delete 关键字的工作方式相同。有一个名为 operator delete 的释放函数负责处理内存,还有一个 delete 运算符负责调用对象析构函数并释放内存。但是,operator newoperator delete 可以在这些上下文之外使用,以代替 C 的 mallocfree,例如。

There is a very subtle difference in C++ between operator new and the new operator. (Read that over again... the ordering is important!)

The function operator new is the C++ analog of C's malloc function. It's a raw memory allocator whose responsibility is solely to produce a block of memory on which to construct objects. It doesn't invoke any constructors, because that's not its job. Usually, you will not see operator new used directly in C++ code; it looks a bit weird. For example:

void* memory = operator new(137); // Allocate at least 137 bytes

The new operator is a keyword that is responsible for allocating memory for an object and invoking its constructor. This is what's encountered most commonly in C++ code. When you write

int* myInt = new int;

You are using the new operator to allocate a new integer. Internally, the new operator works roughly like this:

  1. Allocate memory to hold the requested object by using operator new.
  2. Invoke the object constructor, if any. If this throws an exception, free the above memory with operator delete, then propagate the exception.
  3. Return a pointer to the newly-constructed object.

Because the new operator and operator new are separate, it's possible to use the new keyword to construct objects without actually allocating any memory. For example, the famous placement new allows you to build an object at an arbitrary memory address in user-provided memory. For example:

T* memory = (T*) malloc(sizeof(T)); // Allocate a raw buffer
new (memory) T(); // Construct a new T in the buffer pointed at by 'memory.'

Overloading the new operator by defining a custom operator new function lets you use new in this way; you specify how the allocation occurs, and the C++ compiler will wire it into the new operator.

In case you're curious, the delete keyword works in a same way. There's a deallocation function called operator delete responsible for disposing of memory, and also a delete operator responsible for invoking object destructors and freeing memory. However, operator new and operator delete can be used outside of these contexts in place of C's malloc and free, for example.

你与昨日 2024-10-23 16:46:04

您将 new 表达式与 operator new() 函数混淆了。当前者被编译时,编译器会生成对operator new()函数的调用,并传递足够的大小来保存new表达式中提到的类型,然后传递一个指针返回该类型。

You confuse new expression with operator new() function. When the former is compiled the compiler among other stuff generates a call to operator new() function and passes size enough to hold the type mentioned in the new expression and then a pointer of that type is returned.

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