Partager via


_aligned_malloc

Alloue la mémoire sur une limite d'alignement spécifiée.

void * _aligned_malloc(
    size_t size, 
    size_t alignment
);

Paramètres

  • size
    La taille de l'allocation de mémoire demandée.

  • alignment
    La valeur d'alignement, qui doit être une puissance entière de 2.

Valeur de retour

Un pointeur vers le bloc de mémoire alloué ou NULLsi l'opération a échoué. Le pointeur est un multiple dealignment.

Notes

_aligned_malloc est basé sur malloc.

_aligned_malloc est marqué __declspec(noalias) et __declspec(restrict) ; cela signifie que la fonction ne modifiera pas de variables globales et que le pointeur retourné n'est pas sous la forme d'un alias. Pour plus d'informations, consultez noalias et restrict.

Cette fonction alloue à errno la valeur ENOMEM si l'allocation de mémoire a échoué ou si la taille demandée est supérieure à _HEAP_MAXREQ. Pour plus d'informations sur errno, consultez errno, _doserrno, _sys_errlist et _sys_nerr. Aussi, _aligned_malloc valide ses paramètres. Si alignment n'est pas une puissance de 2 ou que size est nulle, cette fonction appelle le gestionnaire de paramètre non valide, comme décrit dans Validation de paramètre. Si l'exécution est autorisée à se poursuivre, cette fonction renvoie NULL et définit errno avec la valeur EINVAL.

Configuration requise

Routine

En-tête requis

_aligned_malloc

<malloc.h>

Exemple

// 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);
}
  

Voir aussi

Référence

Alignement des données