Whereas malloc
gives you a chunk of memory that could have any alignment (the only requirement is that it must be aligned for the largest primitive type that the implementation supports), posix_memalign
gives you a chunk of memory that is guaranteed to have the requested alignment.
So the result of e.g. posix_memalign(&p, 32, 128)
will be a 128-byte chunk of memory whose start address is guaranteed to be a multiple of 32.
This is useful for various low-level operations (such as using SSE instructions, or DMA), that require memory that obeys a particular alignment.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…