As long as you’re ok with having to call a special function to do the freeing, your approach is okay. I would do your #ifdefs the other way around though: start with the standards-specified options and fall back to platform-specific ones. For example
- If
__STDC_VERSION__ >= 201112Lusealigned_alloc. - If
_POSIX_VERSION >= 200112Luseposix_memalign. - If
_MSC_VERis defined, use the Windows stuff. - …
- If all else fails, just use
malloc/freeand disable SSE/AVX code.
The problem is harder if you want to be able to pass the allocated pointer to free; that’s valid on all the standard interfaces, but not on Windows and not necessarily with the legacy memalign function some unix-like systems have.