0

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"?

Azerum
  • 73
  • 1
  • 2
  • 5

2 Answers2

0

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Unfortunately, the global new operator takes one parameter, so you can not provide it with both "height" and "width". And, the global new/delete operator will be used by not only the code you wrote, but also the library you use, for example std::string, std::vector. So, make such a change may cause nasty problems.

Tiger Yu
  • 744
  • 3
  • 5