Override Placement New?

Started by
11 comments, last by SiCrane 16 years, 9 months ago
Alignment was something I forgot about until I read it in some link on placement new when I was investigating this. Does malloc() always align correctly then?
Advertisement
malloc() and ::operator new() are both guaranteed to return a memory block properly aligned for any type the compiler supports.
Quote:Original post by Zahlman
You could overallocate by (alignment size - 1) bytes and then round up?

That's actually how functions like MSVC's _aligned_malloc() work. However, for static allocations, it's less wasteful to declare the memory block in a union with other types. Then the memory block will be guaranteed to be aligned to the alignment of the most restrictive type in the union. It's still pretty ugly either way.

This topic is closed to new replies.

Advertisement