Is there any standardized function in GCC or glibc to allocate memory block at aligned pointer? Like _align_malloc() in MSVC?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Since the question was asked, a new function was standardized by C11:
and it is available in glibc (not on windows as far as I know). It takes its arguments in the same order as
memalign
, the reverse of Microsoft's_aligned_malloc
, and uses the samefree
function as usual for deallocation.A subtle difference is that
aligned_alloc
requiressize
to be a multiple ofalignment
.It depends on what kind of alignment you expect. Do you want a stricter alignment, or a more relaxed alignment?
malloc
by definition is guaranteed to return a pointer that is properly aligned for storing any standard type in C program (and, therefore, any type built from standard types). Is it what your are looking for? Or do you need something different?There are
_mm_malloc
and_mm_free
which are supported by most compilers of the x86/x64 world, with at least:AFAIK, these functions are not a standard at all. But it is to my knowledge the most supported ones. Other functions are more compiler specific:
There are also C11 standard functions but unfortunately they are not in c++11, and including them in c++ require non standard preprocessor defines...
But not necessarily with other compilers: quoting the standard "The posix_memalign() function is part of the Advisory Information option and need not be provided on all implementations."
See the memalign family of functions.