Ubuntu-16.04 [SOLVED]: Different between aligned malloc and standard malloc?

Ubuntu-16.04 [SOLVED]: Different between aligned malloc and standard malloc?

Home Forums Ubuntu 16.04 Ubuntu-16.04 [SOLVED]: Different between aligned malloc and standard malloc?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #15979

    Anonymous

    QuestionQuestion

    I’m new to malloc and aligned malloc. I know how to use them. However, I don’t know exactly in which case we should use aligned malloc instead of standard malloc. Can you explain to me please? Thank you all.

    #15981

    Anonymous

    Accepted AnswerAnswer

    The glibc documentation makes it reasonably clear where you should use aligned_alloc:

    The address of a block returned by malloc or realloc in GNU systems is always a multiple of eight (or sixteen on 64-bit systems). If you need a block whose address is a multiple of a higher power of two than that, use aligned_alloc or posix_memalign.

    The C standard already guarantees that malloc will return a suitably aligned memory block for any of the standard types but there may be situations in which you want or need stricter alignment.

    As one example, I seem to recall that SSE2 (SIMD) instructions need their data aligned on 16-byte boundaries so you could use aligned_alloc to give you that even on systems where malloc only guarantees alignment to an 8-byte boundary.

    Source: https://stackoverflow.com/questions/39677063/different-between-aligned-malloc-and-standard-malloc
    Author: paxdiablo
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.