0

I'm trying to get better with memory allocation and with use of alignment. I wrote some code to align and allocate memory to dynamic variable of struct type:

 struct alignas(8) k {};
        std::align_val_t al{64};
        k* ptr = new(al) k;
        //delete ptr;    (compiler is telling me to use delete(void*, std::align_val_t) noexcept)

If I wrote this few lines correct, here are my questions: How do I free the memory? Do I have to override delete operator? Also, what is the general purpose of using alignment? Is this just declaring the minimum size of an object? And finally, what is the difference between sizeof() and alignof() operators? Is sizeof returning the size of an object, and alignof returning the size of the biggest variable of the object as space where variable could be stored?

If I made any mistake, could you explain me the correct syntax?
I'm using Visual Studio (C++ 17 in this example)

Raikez
  • 1
  • You don't pass the alignment to the new expression, it's passed to the new operator automatically if necessary. – tkausl Dec 29 '20 at 19:22
  • 1
    Maybe better to start here: [How to invoke aligned new/delete properly?](https://stackoverflow.com/questions/53922209/how-to-invoke-aligned-new-delete-properly) – rustyx Dec 29 '20 at 19:23

0 Answers0