_aligned_malloc

Asigna memoria en un límite de alineación especificado.

Sintaxis

void * _aligned_malloc(
    size_t size,
    size_t alignment
);

Parámetros

size
Tamaño de la asignación de memoria solicitada.

alignment
Valor de la alineación, que debe ser un entero potencia de 2.

Valor devuelto

Puntero al bloque de memoria que se asignó o NULL si se produjo un error en la operación. El puntero es múltiplo de alignment.

Comentarios

_aligned_malloc se basa en malloc.

_aligned_malloc está marcado __declspec(noalias) como y __declspec(restrict), lo que significa que se garantiza que la función no modifique las variables globales y que el puntero devuelto no tenga alias. Para obtener más información, vea noalias y restrict.

Esta función establece errno en ENOMEM si se produce un error en la asignación de memoria o si el tamaño solicitado es mayor que _HEAP_MAXREQ. Para obtener más información sobre errno, consulte 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 cero, esta función invoca al controlador de parámetros no válidos, como se describe en Validación de parámetros. Si la ejecución puede continuar, la función devuelve NULL y establece errno en EINVAL.

Use _aligned_free para desasignar la memoria obtenida por _aligned_malloc y _aligned_offset_malloc. No use free, ya que no reclama la memoria alineada correctamente y puede provocar errores difíciles de diagnosticar.

De manera predeterminada, el estado global de esta función está limitado a la aplicación. Para cambiar este comportamiento, consulte Estado global en CRT.

Requisitos

Routine Encabezado C necesario Encabezado C++
_aligned_malloc <malloc.h> <cstdlib>

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 (((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

Consulte también

Alineación de datos