Aracılığıyla paylaş


_aligned_malloc

Belirtilen hizalama sınırında bellek ayırır.

Sözdizimi

void * _aligned_malloc(
    size_t size,
    size_t alignment
);

Parametreler

size
İstenen bellek ayırmanın boyutu.

alignment
Hizalama değeri, 2 tamsayı gücü olmalıdır.

Dönüş değeri

Ayrılan bellek bloğunun veya NULL işlemin başarısız olup olmadığını gösteren bir işaretçi. İşaretçi, öğesinin alignmentbir katıdır.

Açıklamalar

_aligned_malloc tabanlıdır malloc.

_aligned_mallocve __declspec(restrict)olarak işaretlenir__declspec(noalias), yani işlevin genel değişkenleri değiştirmemesi garanti edilir ve döndürülen işaretçi diğer adla kullanılmaz. Daha fazla bilgi için bkz. noalias ve restrict.

Bu işlev, bellek ayırmanın başarısız olup olmadığını veya istenen boyutun değerinden _HEAP_MAXREQbüyük olup olmadığını olarak ayarlar.errnoENOMEM hakkında errnodaha fazla bilgi için bkz.errno , _doserrno, _sys_errlistve _sys_nerr. Ayrıca parametrelerini _aligned_malloc doğrular. 2'nin gücü değilse veya sıfırsaalignment, bu işlev Parametre doğrulama bölümünde açıklandığı gibi geçersiz parametre işleyicisini size çağırır. Yürütmenin devam etmesi için izin verilirse, bu işlev döndürür NULL ve olarak EINVALayarlanırerrno.

hem _aligned_offset_mallochem de _aligned_malloc tarafından elde edilen belleği serbest bırakmak için kullanın_aligned_free. Hizalanmış belleği doğru şekilde geri kazanmayan ve tanılaması zor hatalara yol açabilen komutunu kullanmayın free.

Varsayılan olarak, bu işlevin genel durumunun kapsamı uygulama olarak belirlenmiştir. Bu davranışı değiştirmek için bkz. CRT'de Genel durum.

Gereksinimler

Yordam Gerekli C üst bilgisi C++ üst bilgisi
_aligned_malloc <malloc.h> <cstdlib>

Örnek

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

Ayrıca bkz.

Veri hizalama