Search code examples
c++multidimensional-arrayoperator-overloadingnew-operatordelete-operator

Is it possible to overload new operator for allocating something like 2d array in C++?


Is it possible to overload global new and global delete operators in C++ for allocation and deallocation of 2d block of memory with given "height" and "width"?


Solution

  • Is it possible to overload new operator for allocating something like 2d array in C++?

    Yes.

    but can I get some code example?

    Example:

    std::unique_ptr<int[][10]> arr {new int[n][10]};
    

    with given "height" and "width"?

    Only if the inner dimensions are compile time constant. Only the outer dimension may be dynamic.

    It is easy to translate between a dynamic single dimensional flat array, and a multi dimensional one.