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"?
Asked
Active
Viewed 88 times
0
-
1memory isnt 2d, but you can allocate a block of size `width*height` – 463035818_is_not_an_ai May 07 '20 at 11:49
-
Sorry, I formulated idea incorrectly. Can I somehow allocate block of size width*height and use it with subscript operators as 2d array? – Azerum May 07 '20 at 11:55
-
https://stackoverflow.com/q/936687/1216776 – stark May 07 '20 at 13:43
2 Answers
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