C++ Logo

std-discussion

Advanced search

Size of heap allocated array

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Tue, 14 Apr 2026 10:46:55 +0200
If I have a pointer that I can call `delete[]` on it, then why does
the standard not allow
getting the size of this storage aside in case of deleting it?
like:

```
auto* p = new Foo[100]{};
auto size = std::get_allocated_elements(p);
delete[] p;
assert(size == 100);
```

Only case where I could see this not working is `new char[]` as it could skip
this metadata as no destructor call is needed for elements.

There are other limitations that prevent the standard to have it?

Received on 2026-04-14 08:47:06