zoukankan      html  css  js  c++  java
  • 学习c/c++之operator new(仅供参考)

    operator new

     
    function
    void* operator new (std::size_t size) throw (std::bad_alloc);
    void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw();
    void* operator new (std::size_t size, void* ptr) throw();


    Allocate storage space

    The first version allocates size bytes of storage space, aligned to represent an object of that size, and returns a non-null pointer to the first byte of this block. On failure, it throws a bad_alloc exception.

    The second version is the nothrow version. It does the same as the first version, except that on failure it returns a null pointer instead of throwing an exception.

    The third version is the placement version, that does not allocate memory - it simply returns ptr. Notice though that the constructor for the object (if any) will still be called by the operator expression.

    Global dynamic storage operator functions are special in the standard library:

    • All three versions of operator new are declared in the global namespace, not in the std namespace.
    • The first and second versions are implicitly declared in every translation unit of a C++ program: The header <new> does not need to be included for them to be present.
    • The first and second versions are also replaceable: A program may provide its own definition, that replaces the default one, to produce the result described above.


    If set_new_handler has been used to define a new_handler function, this new_handler function is called by the standard default definition of operator new if it cannot allocate the requested storage by its own.

    operator new can be called explicitly as a regular function, but in C++, new is an operator with a very specific behavior: An expression with the new operator, first calls function operator new with the size of its type specifier as first argument, and if this is successful, it then automatically initializes or constructs the object (if needed). Finally, the expression evaluates as a pointer to the appropriate type.

     
     

    Parameters

    size
    Size in bytes of the requested memory block.
    size_t is an integral type.
    nothrow_constant
    The constant nothrow.
    This parameter is only used to distinguish it from the first version with an overloaded version. When the nothrow constant is passed as second parameter to operator newoperator new returns a null-pointer on failure instead of throwing a bad_allocexception.
    nothrow_t is the type of constant nothrow.
    ptr
    A pointer to a memory block where the object is to be constructed

    Return value

    For the first and second versions, a pointer to the newly allocated storage space.
    For the third version, ptr is returned.


    Example

    // operator new example
    #include <iostream>
    #include <new>
    using namespace std;

    struct myclass {myclass() {cout <<"myclass constructed\n";}};

    int main () {

    int * p1 = new int;
    // same as:
    // int * p1 = (int*) operator new (sizeof(int));

    int * p2 = new (nothrow) int;
    // same as:
    // int * p2 = (int*) operator new (sizeof(int),nothrow);

    myclass * p3 = (myclass*) operator new (sizeof(myclass));
    // (!) not the same as:
    // myclass * p3 = new myclass;
    // (constructor not called by function call, even for non-POD types)

    new (p3) myclass; // calls constructor
    // same as:
    // operator new (sizeof(myclass),p3)

    return 0;
    }



    Output:

     

    myclass constructed

     
  • 相关阅读:
    剑指offer——关于排序算法的应用(一):归并排序
    剑指offer——关于排序算法的应用:选择排序和冒泡排序
    剑指offer:将矩阵选择、螺旋输出矩阵——Python之光
    剑指offer:链表——常见的多指针协同操作:
    剑指Offer:编程习惯篇:代码鲁棒性,代码可扩展性——防御性的编程习惯,解决问题时方法分模块考虑
    剑指offer:数字二进制含1个数,快速幂运算:二进制位运算的运用
    剑指offer:斐波那契数列,跳台阶,变态跳台阶——斐波那契数列类题目:
    回溯法实现各种组合的检索:
    剑指offer:二维数组中查找
    jdk生成https证书的方法
  • 原文地址:https://www.cnblogs.com/tiankonguse/p/new.html
Copyright © 2011-2022 走看看