:: operator new(size_t)是否使用malloc()? [英] Does ::operator new(size_t) use malloc()?

查看:140
本文介绍了:: operator new(size_t)是否使用malloc()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

::operator new(size_t)是在内部调用malloc(),还是直接使用系统调用/特定于OS的库调用? C ++标准怎么说?

Does ::operator new(size_t) call malloc() internally, or does it use system calls / OS-specific library calls directly? What does the C++ standard say?

此答案中表示:

保证

malloc()返回针对任何标准类型对齐的地址. ::operator new(n)仅保证返回不大于n的任何标准类型对齐的地址,如果T不是字符类型,则仅要求新的T[n]返回与T对齐的地址

malloc() is guaranteed to return an address aligned for any standard type. ::operator new(n) is only guaranteed to return an address aligned for any standard type no larger than n, and if T isn't a character type then new T[n] is only required to return an address aligned for T.

这表明可以不需要new()来调用malloc().

And that suggests that new() cannot be required to call malloc().

注意:这里有一个问题关于operator new除分配以外的其他操作.

Note: There's an SO question about everything operator new does other than allocation.

推荐答案

operator new的实现方式的详细信息是标准库的特定实现的属性-甚至不是编译器或操作系统.我熟悉一个(gnu),并且知道另外3个-CLang,Apache和MSFT.他们所有人都在operator new中使用malloc(),因为它使图书馆开发人员的工作变得如此轻松.

The details of how operator new is implemented are property of a particular implementation of standard library - not even a compiler or operation system. I am familiar with one (gnu) and aware of 3 others - CLang, Apache and MSFT. All of them are using malloc() within operator new, because it just makes a life of library developer so much easier.

如果不使用malloc(),则表示开发人员将不得不在内存分配方面重新实现很多功能,并在代码中大量使用依赖于OS的逻辑来实际请求内存.当malloc()已经存在时,没有人愿意这样做.但是他们绝没有义务使用它.

If malloc() were not used, said developer would have to reimplement a lot in terms of memory allocation, and sprinkle the code heavily with OS-dependent logic to actually request memory. No one wants to do this when malloc() is already there. But by no means they are obliged to use it.

这篇关于:: operator new(size_t)是否使用malloc()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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