mmozeiko
Instead of all this manual work or wrappers you could simply use modern compiler features like Address Sanitizer. It automatically will check that pointer dereference is to valid/allocated memory. And it will do that without any extra work from user - you can pass pointer through countless functions, store it in heap and get it back later, offset by some amount - and runtime will still check that it is valid once it is dereferenced. And error message it will produce will be way friendlier than any manual wrapper can do, as it will tell location of both - where the invalid dereference happens, and where the original allocation happened. It's super useful in both - C and C++.
Sure, but my point was that the compiler and its tools only know the bound of actual memory allocations, so it will take longer before the bug is detected from an actual invalid access due to padding. C++ template collections and such allow considering the tighter length variable when checking for out of bound errors as soon as a
potentially dangerous access is detected. Being inside of the same valid allocation can still be a memory error if the same bug eventually goes outside the real allocation. Just like you want to detect memory leaks before actually running out of memory. If your algorithm goes out of bound once in a thousand times and it's only without padding once in a thousand times, the error will only happen once in a million times and be very hard to track down.