다음을 통해 공유


_aligned_malloc

지정된 맞춤 경계에 메모리를 할당합니다.

void * _aligned_malloc(
    size_t size, 
    size_t alignment
);

매개 변수

  • size
    요청된 메모리 할당의 크기입니다.

  • alignment
    맞춤 값은 -2의 정수 거듭제곱이어야 합니다.

반환 값

할당 된 메모리 블록에 대한 포인터 또는 작업에 실패한 경우 NULL 입니다. 포인터는 alignment의 배수입니다.

설명

_aligned_mallocmalloc을 기반으로 합니다.

_aligned_malloc__declspec(noalias)__declspec(restrict) 로 표시됩니다. 즉, 함수가 전역 변수를 수정하지 않고 반환된 포인터가 별칭이 지정되지 않도록 보증합니다. 자세한 내용은 noaliasrestrict 를 참조하십시오.

이 함수는 메모리 할당에 실패 한 경우 또는 요청된 된 크기 _HEAP_MAXREQ 보다 큰 경우, errno 를 ENOMEM 로 설정합니다. errno에 대한 자세한 내용은 errno, _doserrno, _sys_errlist 및 _sys_nerr을 참조하십시오. 또한 _aligned_malloc 매개 변수 유효성을 검사 합니다. 만일 alignment 가 2의 거듭제곱수가 아닌 경우, 이 함수는 size 에 설명된 대로 잘못된 매개 변수 처리기를 매개 변수 유효성 검사 에서 호출합니다. 계속해서 실행하도록 허용된 경우, 이 함수는 NULL 를 반환하고 errno 를 EINVAL로 설정합니다.

요구 사항

루틴

필수 헤더

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

참고 항목

참조

데이터 맞춤