malloc_usable_size()返回错误的大小
发布时间:2020-12-16 10:06:52 所属栏目:百科 来源:网络整理
导读:我想知道malloc分配的大小. 我在下面写了源代码. test.c的 #include stdio.h#include stdint.h#include malloc.hvoid main(void){ uint8_t *test; test = (uint8_t *)malloc(sizeof(uint8_t)*4); printf("sizeof(test) = %dn",malloc_usable_size(test)); f
我想知道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) = %dn",malloc_usable_size(test)); free(test); } 我预计大小为4. sizeof(test) = 12 你能告诉我什么是错的吗? 解决方法malloc_usable_size(test) 上述函数返回的值不是您所要求的.它可能大于请求的分配大小,具体取决于cpu字节的排序和对齐.这完全取决于底层实施. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |