_aligned_malloc

針對指定的對齊界限配置記憶體。

語法

void * _aligned_malloc(
    size_t size,
    size_t alignment
);

參數

size
要求的記憶體配置大小。

alignment
對齊值,必須是 2 的整數冪。

傳回值

已配置之記憶體區塊的指標,或為 NULL (作業失敗時)。 指標是 alignment 的倍數。

備註

_aligned_malloc 是以 malloc 為基礎。

_aligned_malloc 標示 __declspec(noalias) 為 和 __declspec(restrict) ,這表示函式保證不會修改全域變數,且傳回的指標不會有別名。 如需詳細資訊,請參閱 noaliasrestrict

若記憶體配置失敗,或是要求的大小大於 errno,則此函式會將 ENOMEM 設為 _HEAP_MAXREQ。 如需 的詳細資訊 errno ,請參閱 errno_doserrno_sys_errlist_sys_nerr 。 此外,_aligned_malloc 也會驗證其參數。 如果 alignment 不是 2 或 size 零的電源,則此函式會叫用不正確參數處理常式,如參數驗證 中所述 。 若允許繼續執行,此函式會傳回 NULL,並將 errno 設為 EINVAL

使用 _aligned_free 來解除配置 和 _aligned_offset_malloc 取得的 _aligned_malloc 記憶體。 請勿使用 free ,這不會正確回收對齊的記憶體,而且可能會導致難以診斷的錯誤。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的 C 標頭 C++ 標頭
_aligned_malloc <malloc.h> <cstdlib>

範例

// crt_aligned_malloc.c

#include <malloc.h>
#include <stdio.h>

int main() {
    void    *ptr;
    size_t  alignment,
            off_set;

    // Note alignment should be 2^N where N is any positive int.
    alignment = 16;
    off_set = 5;

    // Using _aligned_malloc
    ptr = _aligned_malloc(100, alignment);
    if (ptr == NULL)
    {
        printf_s( "Error allocation aligned memory.");
        return -1;
    }
    if (((unsigned long long)ptr % alignment ) == 0)
        printf_s( "This pointer, %p, is aligned on %zu\n",
                  ptr, alignment);
    else
        printf_s( "This pointer, %p, is not aligned on %zu\n",
                  ptr, alignment);

    // Using _aligned_realloc
    ptr = _aligned_realloc(ptr, 200, alignment);
    if ( ((unsigned long long)ptr % alignment ) == 0)
        printf_s( "This pointer, %p, is aligned on %zu\n",
                  ptr, alignment);
    else
        printf_s( "This pointer, %p, is not aligned on %zu\n",
                  ptr, alignment);
    _aligned_free(ptr);

    // Using _aligned_offset_malloc
    ptr = _aligned_offset_malloc(200, alignment, off_set);
    if (ptr == NULL)
    {
        printf_s( "Error allocation aligned offset memory.");
        return -1;
    }
    if ( ( (((unsigned long long)ptr) + off_set) % alignment ) == 0)
        printf_s( "This pointer, %p, is offset by %zu on alignment of %zu\n",
                  ptr, off_set, alignment);
    else
        printf_s( "This pointer, %p, does not satisfy offset %zu "
                  "and alignment %zu\n",ptr, off_set, alignment);

    // Using _aligned_offset_realloc
    ptr = _aligned_offset_realloc(ptr, 200, alignment, off_set);
    if (ptr == NULL)
    {
        printf_s( "Error reallocation aligned offset memory.");
        return -1;
    }
    if ( ( (((unsigned long long)ptr) + off_set) % alignment ) == 0)
        printf_s( "This pointer, %p, is offset by %zu on alignment of %zu\n",
                  ptr, off_set, alignment);
    else
        printf_s( "This pointer, %p, does not satisfy offset %zu and "
                  "alignment %zu\n", ptr, off_set, alignment);

    // Note that _aligned_free works for both _aligned_malloc
    // and _aligned_offset_malloc. Using free is illegal.
    _aligned_free(ptr);
}
This pointer, 3280880, is aligned on 16
This pointer, 3280880, is aligned on 16
This pointer, 3280891, is offset by 5 on alignment of 16
This pointer, 3280891, is offset by 5 on alignment of 16

另請參閱

資料對齊方式