Does dynamic memory allocation differ in C and C++ in popular implementations?

后端 未结 5 908
情书的邮戳
情书的邮戳 2020-12-04 12:17

As far as the respective language standards go, C offers dynamic memory allocation only through the malloc() family, while in C++ the most common form of alloca

5条回答
  •  遥遥无期
    2020-12-04 12:34

    glibc new operator is a thin wrapper around malloc. And glibc malloc uses different strategies for different size allocation requests. You can see the implementation, or at least the comments here.

    Here's an excerpt from the comments in malloc.c:

    /*
    47   This is not the fastest, most space-conserving, most portable, or
    48   most tunable malloc ever written. However it is among the fastest
    49   while also being among the most space-conserving, portable and tunable.
    50   Consistent balance across these factors results in a good general-purpose
    51   allocator for malloc-intensive programs.
    52 
    53   The main properties of the algorithms are:
    54   * For large (>= 512 bytes) requests, it is a pure best-fit allocator,
    55     with ties normally decided via FIFO (i.e. least recently used).
    56   * For small (<= 64 bytes by default) requests, it is a caching
    57     allocator, that maintains pools of quickly recycled chunks.
    58   * In between, and for combinations of large and small requests, it does
    59     the best it can trying to meet both goals at once.
    60   * For very large requests (>= 128KB by default), it relies on system
    61     memory mapping facilities, if supported.
    */
    

提交回复
热议问题