From 3884a2ce457e4073964a1aefcf34c80a5b0df66e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 18 Oct 2015 04:19:10 +0200 Subject: info: malloc aligns pointers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- doc/info/chap/memory-allocation.texinfo | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/doc/info/chap/memory-allocation.texinfo b/doc/info/chap/memory-allocation.texinfo index 2a9f8dc..cc88dcf 100644 --- a/doc/info/chap/memory-allocation.texinfo +++ b/doc/info/chap/memory-allocation.texinfo @@ -389,6 +389,18 @@ You do not need to write int* ten_integers = (int*)malloc(10 * sizeof(int)); @end example +@tpindex max_align_t +@sc{ISO}@tie{}C11 defines the auxiliary data +type @code{max_align_t} which has a size +the guaranteed to be a power-of-two (possibly +1) multiple of the size of any intrinsic data +type defined by the C standard. Its size a +suitable aligment for any pointer type. +@code{malloc}, even before C11, uses its size +for the aligment of all pointers it returns. +@code{malloc(n)} is hence equivalent to +@code{aligned_alloc(sizeof(max_align_t), n)}. + @item void* calloc(size_t count, size_t size) @fnindex calloc @cpindex Memory allocation without uninitialisation @@ -664,20 +676,6 @@ A recommended practice, to align pointers is: @example p = aligned_alloc(sizeof(*p), n) @end example - -@tpindex max_align_t -@sc{ISO}@tie{}C11 defines the auxiliary data -type @code{max_align_t} which has a size -the guaranteed to be a power-of-two (possibly -1) multiple of the size of any intrinsic data -type defined by the C standard. Its size it -hence suitable for the aligment of any pointer -to such data type. It can thus be a good idea -to try replacing @code{malloc} with the macro -@example -#define malloc(n) \ - aligned_alloc(sizeof(max_align_t), n) -@end example @end table -- cgit v1.2.3-70-g09d2