我想知道malloc分配的大小.
我在下面写了源代码.
#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);
}
Run Code Online (Sandbox Code Playgroud)
我预计大小为4.
但结果是12.
sizeof(test) = 12
Run Code Online (Sandbox Code Playgroud)
你能告诉我什么是错的吗?
我希望4号正确出来.
malloc_usable_size(test)
Run Code Online (Sandbox Code Playgroud)
上述函数返回的值不是您所要求的.它可能大于请求的分配大小,具体取决于cpu字节的排序和对齐.这完全取决于底层实施.