Share via


_aligned_malloc

指定された配置境界にメモリを割り当てます。

void * _aligned_malloc(
    size_t size, 
    size_t alignment
);

パラメーター

  • size
    要求されたメモリ割り当てのサイズ。

  • alignment
    2. の整数乗する必要がある配置の値。

戻り値

操作が失敗した場合または NULL 割り当てられたメモリ ブロックへのポインター。ポインターは alignment の倍数です。

解説

_aligned_mallocmalloc に基づいています。

_aligned_malloc はグローバル変数を変更しないように関数が保証できない返されたポインターが JIT エイリアス化したことを意味するのマークされた __declspec(noalias)__declspec(restrict) です。詳細についてはnoalias制限します。 を参照してください。

メモリ割り当てに失敗したり要求が _HEAP_MAXREQ サイズよりも大きい場合は関数のセット errnoENOMEMこの。errno の詳細については、「errno、_doserrno、_sys_errlist、および _sys_nerr」を参照してください。また**_aligned_malloc** はパラメーターを検証します。alignment が 2 の累乗がない場合または size がゼロの場合この関数は パラメーターの検証 に説明されているように無効なパラメーター ハンドラーを呼び出します。実行の継続が許可された場合、この関数は NULL を返し、errno を EINVAL に設定します。

必要条件

ルーチン

必須ヘッダー

_aligned_malloc

<malloc.h>

使用例

// 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 (((int)ptr % alignment ) == 0)
        printf_s( "This pointer, %d, is aligned on %d\n",
                  ptr, alignment);
    else
        printf_s( "This pointer, %d, is not aligned on %d\n", 
                  ptr, alignment);

    // Using _aligned_realloc
    ptr = _aligned_realloc(ptr, 200, alignment);
    if ( ((int)ptr % alignment ) == 0)
        printf_s( "This pointer, %d, is aligned on %d\n",
                  ptr, alignment);
    else
        printf_s( "This pointer, %d, is not aligned on %d\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 ( ( (((int)ptr) + off_set) % alignment ) == 0)
        printf_s( "This pointer, %d, is offset by %d on alignment of %d\n",
                  ptr, off_set, alignment);
    else
        printf_s( "This pointer, %d, does not satisfy offset %d "
                  "and alignment %d\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 ( ( (((int)ptr) + off_set) % alignment ) == 0)
        printf_s( "This pointer, %d, is offset by %d on alignment of %d\n",
                  ptr, off_set, alignment);
    else
        printf_s( "This pointer, %d, does not satisfy offset %d and "
                  "alignment %d\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);
}
  

参照

関連項目

データの整列