_aligned_malloc

Aloca memória em um limite de alinhamento especificado.

Sintaxe

void * _aligned_malloc(
    size_t size,
    size_t alignment
);

Parâmetros

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

alignment
O valor de alinhamento, que deve ser um inteiro elevado à segunda potência.

Retornar valor

Um ponteiro para o bloco de memória alocado ou NULL, se a operação tiver falhado. O ponteiro é um múltiplo de alignment.

Comentários

_aligned_malloc baseia-se em malloc.

_aligned_malloc é marcado __declspec(noalias) e , o que significa que a função tem a garantia de não modificar variáveis globais e __declspec(restrict)que o ponteiro retornado não é aliased. Para obter mais informações, consulte noalias e restrict.

Essa função define errno como ENOMEM se a alocação da memória tiver falhado ou se o tamanho solicitado for maior que _HEAP_MAXREQ. Para obter mais informações sobre errno, consulte errno, _doserrno, _sys_errlist e _sys_nerr. Além disso, _aligned_malloc valida seus parâmetros. Se alignment não for uma potência de 2 ou size for zero, essa função invocará o manipulador de parâmetros inválido, conforme descrito em Validação de parâmetro. Se a execução puder continuar, essa função retornará NULL e definirá errno como EINVAL.

Use _aligned_free para desalocar a memória obtida por _aligned_malloc e _aligned_offset_malloc. Não use free, que não recupera a memória alinhada corretamente e pode levar a bugs difíceis de diagnosticar.

Por padrão, o estado global dessa função tem como escopo o aplicativo. Para alterar esse comportamento, consulte Estado global na CRT.

Requisitos

Rotina Cabeçalho C necessário Cabeçalho C++
_aligned_malloc <malloc.h> <cstdlib>

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

Confira também

Alinhamento de dados