As an answer to Idiom for initializing an std::array using a generator function taking the index?, I proposed a way to obtain a properly aligned storage.
Yet I think that my solution can be simplified, using this signature:
void* operator new[]( std::size_t count, std::align_val_t al );
Which I use for some type T
like this:
T *p = new(std::align_val_t(alignof(T))) T[N];
Yet I cannot find the corresponding way to deallocate.
The expected signature(s) may be:
void operator delete[]( void* ptr,std::align_val_t al ) noexcept;
void operator delete[]( void* ptr, std::size_t sz,std::align_val_t al ) noexcept;
But I couldn’t find the proper way to call it. I tried, for instance:
// sanitizer: new-delete-type-mismatch
delete[] (N * sizeof(N), std::align_val_t{alignof(Int)}, p);
// sanitizer: new-delete-type-mismatch
delete[] (std::align_val_t{alignof(Int)}, p);
//error: type 'enum class std::align_val_t' argument given to 'delete', expected pointer
delete[] (p, N * sizeof(N), std::align_val_t{alignof(Int)});
//error: type 'enum class std::align_val_t' argument given to 'delete', expected pointer
delete[] (p, std::align_val_t{alignof(Int)});
//error: type 'enum class std::align_val_t' argument given to 'delete', expected pointer
// sanitizer attempting free on address which was not malloc()-ed
::operator delete[](p, std::align_val_t{alignof(Int)});
// sanitizer attempting free on address which was not malloc()-ed
::operator delete[](p, N * sizeof(N), std::align_val_t{alignof(Int)});
but no version is compiling or accepted by the sanitizer.
NB msvc fails to compile for all of them.
What is the correct way to use array version of new/delete with std::align_val_t
?
NB How to call new[]/delete[] operator with aligned memory seems a dup but the error the op get is not the same and there don’t seem to be a proper answer.
How to invoke aligned new/delete properly?, and especially this answer does not solve my issue (I tested the linked answer, which lead to sanitizer complaint)