I don't understand why you only conditionally increment location
after an allocation, and if you increment location
, then the function doesn't execute a return
statement which is undefined behavior.
Your deallocation strategy is completely broken unless objects are only deallocated in the exact opposite order of allocations. Additionally, after the array element has been used for one allocation, it won't ever be used again due to the assignment in the deallocator.
As for the actual question: the first Scalar
allocated is allocated at the same location as the buffer, so the first Scalar
and buf[0]
share the same memory. As they're both composed of a single int
, writes to one might be readable from the other. And when you construct the Scalar
, it assigns the value 11
to the shared memory. I think doing this is undefined behavior, but I'm not certain.