I was implementing a stack allocator and overloaded the operator new[]
for a certain class to use my own allocator. I then noticed that operator new[]
allocated memory for the amount of elements in the allocated array.
So for example:
test_class* test_arr = new test_class[5];
requests 8 bytes + 5*sizeof(test_class) from my allocator and in the first 8 bytes it stores the size of the array, namely 5 in this case.
Why does it do that? It's the job of my allocator to keep track of the amount of memory that was allocated. For that part it doesn't really make sense, does it? So what's the point? Also, can (or shouldn't I?) I somehow "turn that off"?