Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      aligned_alloc

      From cppreference.com
      <c‎ |memory
       
       
       
      Defined in header<stdlib.h>
      void*aligned_alloc(size_t alignment,size_t size);
      (since C11)

      Allocatesize bytes of uninitialized storage whose alignment is specified byalignment. Thesize parameter must be an integral multiple ofalignment.

      aligned_alloc is thread-safe: it behaves as though only accessing the memory locations visible through its argument, and not any static storage.

      A previous call tofree,free_sized, andfree_aligned_sized(since C23) orrealloc that deallocates a region of memorysynchronizes-with a call toaligned_alloc that allocates the same or a part of the same region of memory. This synchronization occurs after any access to the memory by the deallocating function and before any access to the memory byaligned_alloc. There is a single total order of all allocation and deallocation functions operating on each particular region of memory.

      Contents

      [edit]Parameters

      alignment - specifies the alignment. Must be a valid alignment supported by the implementation.
      size - number of bytes to allocate. An integral multiple ofalignment

      [edit]Return value

      On success, returns the pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated withfree orrealloc.

      On failure, returns a null pointer.

      [edit]Notes

      Passing asize which is not an integral multiple ofalignment or analignment which is not valid or not supported by the implementation causes the function to fail and return a null pointer (C11, as published, specified undefined behavior in this case, this was corrected byDR460). Removal of size restrictions to make it possible to allocate small objects at restrictive alignment boundaries (similar toalignas) has been proposed byN2072.

      As an example of the "supported by the implementation" requirement, POSIX functionposix_memalign accepts anyalignment that is a power of two and a multiple ofsizeof(void*), and POSIX-based implementations ofaligned_alloc inherit this requirements.

      Fundamental alignments are always supported. Ifalignment is a power of two and not greater than_Alignof(max_align_t),aligned_alloc may simply callmalloc.

      Regularmalloc aligns memory suitable for any object type with a fundamental alignment. Thealigned_alloc is useful for over-aligned allocations, such as toSSE, cache line, orVM page boundary.

      This function is not supported in Microsoft C Runtime library because its implementation ofstd::free isunable to handle aligned allocations of any kind. Instead, MS CRT provides_aligned_malloc (to be freed with_aligned_free).

      [edit]Example

      Run this code
      #include <stdio.h>#include <stdlib.h> int main(void){int*p1=malloc(10*sizeof*p1);printf("default-aligned addr:   %p\n",(void*)p1);free(p1); int*p2= aligned_alloc(1024,1024*sizeof*p2);printf("1024-byte aligned addr: %p\n",(void*)p2);free(p2);}

      Possible output:

      default-aligned addr:   0x1e40c201024-byte aligned addr: 0x1e41000

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.22.3.1 The aligned_alloc function (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.22.3.1 The aligned_alloc function (p: 253)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.22.3.1 The aligned_alloc function (p: 347-348)

      [edit]See also

      C++ documentation foraligned_alloc
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/memory/aligned_alloc&oldid=157842"

      [8]ページ先頭

      ©2009-2025 Movatter.jp