_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_malloc_aligned_offset_malloc 获取的内存。 请不要使用 free,它不会正确回收对齐的内存,并且可能导致难以诊断的 bug。

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 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

另请参阅

数据对齐