What's the purpose of having a separate “operator new[]”?

前端 未结 5 1865
天涯浪人
天涯浪人 2020-12-11 15:44

Looks like operator new and operator new[] have exactly the same signature:

void* operator new( size_t size );
void* operator new[]         


        
5条回答
  •  忘掉有多难
    2020-12-11 16:26

    Standard says that new T calls operator new( ) and new T[ ] results in a call of operator new[]( ). You could overload them if you want. I believe that there is no difference between them by default. Standard says that they are replaceable (3.7.3/2):

    The library provides default definitions for the global allocation and deallocation functions. Some global allocation and deallocation functions are replaceable (18.4.1). A C + + program shall provide at most one definition of a replaceable allocation or deallocation function. Any such function definition replaces the default version provided in the library (17.4.3.4). The following allocation and deallocation functions (18.4) are implicitly declared in global scope in each translation unit of a program

    void* operator new(std::size_t) throw(std::bad_alloc);  
    void* operator new[](std::size_t) throw(std::bad_alloc);  
    void operator delete(void*) throw();  
    void operator delete[](void*) throw();  
    

提交回复
热议问题