指定された配置の境界にメモリを割り当てます。
構文
void * _aligned_malloc(
size_t size,
size_t alignment
);
パラメーター
size
要求されたメモリ割り当てのサイズ。
alignment
アラインメント値。2 の整数乗である必要があります。
戻り値
割り当てられたメモリ ブロックへのポインター。操作が失敗した場合は NULL。 ポインターは、alignment の倍数です。
解説
_aligned_malloc は malloc に基づきます。
_aligned_malloc は __declspec(noalias) および __declspec(restrict)マークされます。つまり、関数はグローバル変数を変更しないことが保証され、返されるポインターはエイリアス化されません。 詳細については、次のトピックを参照してください。 noalias および restrict
この関数は、メモリ割り当てが失敗するか、要求されたサイズが errno より大きかった場合に、ENOMEM を _HEAP_MAXREQ に設定します。 errno の詳細については、「errno、_doserrno、_sys_errlist、_sys_nerr」を参照してください。 また、_aligned_malloc はそのパラメーターを検証します。 alignmentが 2 の累乗でない場合、または size が 0 の場合、この関数は無効なパラメーター ハンドラーを呼び出します(パラメーター検証で説明します。 実行の継続が許可された場合、この関数は NULL を返し、errno を EINVAL に設定します。
_aligned_malloc と _aligned_offset_malloc の両方で取得したメモリの割り当てを解除するには、_aligned_free を使用します。 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