_aligned_malloc
Asigna memoria en un límite de alineación especificado.
void * _aligned_malloc(
size_t size,
size_t alignment
);
Parámetros
size
Tamaño de asignación de memoria solicitada.alignment
El valor alignment, que debe ser una potencia entero de 2.
Valor devuelto
Un puntero al bloque de memoria que fue asignado o a NULL si se produjo un error en la operación.el puntero es un múltiplo de alignment.
Comentarios
_aligned_malloc se basa en malloc.
_aligned_malloc es __declspec(noalias) marcado y __declspec(restrict), lo que significa que la función está garantizada para no modificar variables globales y que el puntero devuelto no es con alias.Para obtener más información, vea noalias y limitado.
Esta función establece errno a ENOMEM aunque falle la asignación de memoria o si el tamaño solicitado era mayor que _HEAP_MAXREQ.Para obtener más información sobre errno, vea errno, _doserrno, _sys_errlist, y _sys_nerr.Además, _aligned_malloc valida sus parámetros.Si alignment no es una potencia de 2 o size es cero, esta función invoca el controlador no válido de parámetro, tal y como se describe en Validación de parámetros.Si la ejecución puede continuar, esta función devuelve NULL y establece errno a EINVAL.
Requisitos
rutina |
Encabezado necesario |
---|---|
_aligned_malloc |
<malloc.h> |
Ejemplo
// 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);
}