malloc_usable_size()返回错误的大小

时间:2017-06-16 05:02:12

标签: c memory malloc

我想知道malloc分配的大小。
我在下面写了源代码。

test.c的

#include <stdio.h>
#include <stdint.h>
#include <malloc.h>
void main(void)
{
    uint8_t *test;

    test = (uint8_t *)malloc(sizeof(uint8_t)*4);
    printf("sizeof(test) = %d\n",malloc_usable_size(test));

    free(test); 
}  

我预计大小为4.
但结果是12。

sizeof(test) = 12

你能告诉我什么是错的吗? 我希望正确的4号出现。

1 个答案:

答案 0 :(得分:7)

malloc_usable_size(test)

上述函数返回的值不是您所要求的。它可能大于请求的分配大小,具体取决于cpu字节的排序和对齐。这完全取决于底层实施。