malloc_usable_size()返回错误的大小 [英] malloc_usable_size() returns the wrong size

查看:721
本文介绍了malloc_usable_size()返回错误的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道malloc分配的大小.
我已经在下面编写了源代码.

I want to know the size allocated by malloc.
I have written the source code below.

#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.

I expected size to be 4.
But the result is 12.

sizeof(test) = 12

你能告诉我怎么了吗?
我希望4号尺寸正确.

Can you tell me what's wrong?
I hope that size 4 correctly comes out.

推荐答案

malloc_usable_size(test)

上述函数返回的值未按您的要求固定.它可能大于请求的分配大小,具体取决于cpu字节的排序和对齐方式.这完全取决于底层的实现.

The value returned by above function is not fixed as you requested. it may be greater than the requested size of the allocation depending upon the cpu byte ordering and alignment. this is totally depend upon the underlaying implementation.

这篇关于malloc_usable_size()返回错误的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆