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

后端 未结 5 909
情书的邮戳
情书的邮戳 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:33

    On Visual C++, stepping into a new expression leads me to this snippet in new.cpp:

    #include 
    #include 
    
    _C_LIB_DECL
    int __cdecl _callnewh(size_t size) _THROW1(_STD bad_alloc);
    _END_C_LIB_DECL
    
    void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
            {       // try to allocate size bytes
            void *p;
            while ((p = malloc(size)) == 0)
                    if (_callnewh(size) == 0)
                    {       // report no memory
                    static const std::bad_alloc nomem;
                    _RAISE(nomem);
                    }
    
            return (p);
            }
    

    So VC++'s new also wraps the malloc() call.

提交回复
热议问题