posix_memalign vs. malloc

#include 
#include 
//#include 
#include 
#include  // for posix_memalign()
void test()
{
    char* buf;
    int ret;
    int align = 4096;
    uintptr_t align2 = 4096;
    int alc_size = 17;
    char* buf2 = (char*)malloc(alc_size);
    ret = posix_memalign((void**)&buf, align, alc_size);
    if(ret){
        errno = ret;
        perror("posix_memalign");
        return ;
    }
    printf("%p %llx\n", buf, (unsigned long long)buf);
    printf("%llu %llu\n", (unsigned long long)buf % align, (unsigned long long)buf2 % align);
    printf("%08" PRIdPTR ":" "%08" PRIdPTR "\n", (uintptr_t)buf % align2, (uintptr_t)buf2 % align2);
    
    free(buf);
    free(buf2);
}
int main()
{
    test();
    return 0;
}
work:[TestKit]$./test 
0x1ca4000 1ca4000
0 16
00000000:00000016

你可能感兴趣的:(posix_memalign vs. malloc)