_aligned_malloc

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

Syntaxe

void * _aligned_malloc(
    size_t size,
    size_t alignment
);

Paramètres

size
Taille de l'allocation de mémoire demandée.

alignment
Valeur d'alignement, qui doit être un entier à puissance 2.

Valeur retournée

Pointeur vers le bloc de mémoire qui a été alloué ou NULL si l'opération a échoué. Le pointeur est un multiple de alignment.

Notes

_aligned_malloc est basé sur malloc.

_aligned_malloc est marqué __declspec(noalias) et __declspec(restrict), ce qui signifie que la fonction est garantie de ne pas modifier les variables globales et que le pointeur retourné n’est pas alias. Pour plus d’informations, consultez noalias et restrict.

Cette fonction affecte à errno la valeur ENOMEM si l'allocation de mémoire a échoué ou si la taille demandée était supérieure à _HEAP_MAXREQ. Pour plus d’informations sur errno, voir errno, _doserrno, , _sys_errlistet _sys_nerr. De plus, _aligned_malloc valide ses paramètres. Si alignment la puissance 2 n’est pas égale ou égale à size zéro, cette fonction appelle le gestionnaire de paramètres non valide, comme décrit dans la validation des paramètres. Si l'exécution est autorisée à se poursuivre, cette fonction retourne NULL et affecte la valeur errno à EINVAL.

Permet _aligned_free de libérer la mémoire obtenue par les deux _aligned_malloc et _aligned_offset_mallocpar . N’utilisez freepas , qui ne récupère pas correctement la mémoire alignée et peut entraîner des bogues difficiles à diagnostiquer.

Par défaut, l’état global de cette fonction est limité à l’application. Pour modifier ce comportement, consultez État global dans le CRT.

Spécifications

Routine En-tête C requis En-tête C++
_aligned_malloc <malloc.h> <cstdlib>

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

Voir aussi

Alignement des données