加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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.
但结果是12.

sizeof(test) = 12

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

解决方法

malloc_usable_size(test)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读