_aligned_malloc

Aloca memória em um limite de alinhamento especificado.

void * _aligned_malloc(
    size_t size, 
    size_t alignment
);

Parâmetros

  • size
    dimensionar da alocação de memória solicitada.

  • alignment
    O valor de alinhamento, que deve ser uma potência inteira de 2.

Valor de retorno

Um ponteiro para o bloco de memória que foi alocado ou NULLSe a operação falhou. O ponteiro é um múltiplo de alignment.

Comentários

_aligned_malloc baseia-se em malloc.

_aligned_malloc está marcado como __declspec(noalias) e __declspec(restrict), que significa que a função tem a garantia não modificar variáveis global e que o ponteiro retornado não é com alias. Para obter mais informações, consulte noalias and restringir.

In Visual C++ 2005, define essa função errno para ENOMEM Se a falha na alocação de memória ou se era maior do que o dimensionar solicitado _HEAP_MAXREQ. Para obter mais informações sobre o errno, consulte errno, _doserrno, _sys_errlist e _sys_nerr.Além disso, _aligned_malloc valida os parâmetros. If alignment não é uma potência de 2 ou size é zero, essa função chama o manipulador de parâmetro inválido sistema autônomo descrito em Validação de parâmetro. Se a execução for permitida para continuar, esta função retorna NULL e conjuntos errno para EINVAL.

Requisitos

Rotina

Cabeçalho necessário

_aligned_malloc

<malloc.h>

Exemplo

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

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 também

Referência

Alinhamento de dados