EOF
Développer Réduire
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
Traduction
Source
Ce sujet n'a pas encore été évalué - Évaluez ce sujet

_aligned_malloc

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

void * _aligned_malloc(
    size_t size, 
    size_t alignment
);
size

taille de l'allocation de mémoire demandée.

alignment

La valeur d'inscription, qui doit être une puissance entière à 2.

Un pointeur vers le bloc de mémoire alloué ou à NULL si l'opération a échoué. le pointeur est un multiple d' alignment.

_aligned_malloc est basé sur malloc.

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

Cette fonction définit errno à 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. en outre, _aligned_malloc valide ses paramètres. Si alignment n'est pas une puissance de 2 ou size est zéro, cette fonction appelle le gestionnaire de paramètre non valide, comme décrit dans Validation des paramètres. Si est autorisé à l'exécution de se poursuivre, retourne NULL de cette fonction et définit errno à EINVAL.

routine

en-tête requis

_aligned_malloc

<malloc.h>

// 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);
}
ce pointeur, 3280880, est aligné sur 16
ce pointeur, 3280880, est aligné sur 16
ce pointeur, 3280891, est offset par 5 sur l'alignement de 16
ce pointeur, 3280891, est offset par 5 sur l'alignement de 16
Cela vous a-t-il été utile ?
(1500 caractères restants)

Ajouts de la communauté

AJOUTER
© 2013 Microsoft. Tous droits réservés.