如何获得的大小动态(使用malloc或释放calloc)分配的内存? [英] How to get the size of dynamically (using malloc or calloc )allocated memory?

查看:221
本文介绍了如何获得的大小动态(使用malloc或释放calloc)分配的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1281686/determine-size-of-dynamically-allocated-memory-in-c\">determine在C 结果动态分配的内存大小
  有关的malloc和sizeof 结果新手问题
  <一href=\"http://stackoverflow.com/questions/232691/how-can-i-get-the-size-of-an-array-from-a-pointer-in-c\">How我可以用C的指针获取数组的大小?


  
  

<一个href=\"http://stackoverflow.com/questions/5213356/malloc-how-much-memory-has-been-allocated?rq=1\">Malloc - &GT;多少内存已分配?


  INT ** arrofptr;
arrofptr =(INT **)的malloc(sizeof的为(int *)* 2);
arrofptr [0] =(INT *)malloc的(的sizeof(int)的* 6144);
arrofptr [1] =(INT *)malloc的(的sizeof(int)的* 4800);

现在我必须知道多少字节arrofptr分配,arrofptr [0],arrofptr [1]?有没有办法知道的大小?

如果我们将打印

 的sizeof(arrofptr);
的sizeof(arrofptr [0]);
的sizeof(arrofptr [1]);

然后将打印4


解决方案

没有。如果不使用额外数据的地方,存储分配的大小。

Possible Duplicate:
determine size of dynamically allocated memory in c
newbie questions about malloc and sizeof
How can I get the size of an array from a pointer in C?

Malloc -> how much memory has been allocated?

int **arrofptr;
arrofptr = (int **)malloc(sizeof(int *) * 2);
arrofptr[0] = (int *)malloc(sizeof(int)*6144);
arrofptr[1] = (int *)malloc(sizeof(int)*4800);

Now i have to know that how many bytes are allocated in arrofptr,arrofptr[0],arrofptr[1]? is there any way to know the size?

if we will print

sizeof(arrofptr);
sizeof(arrofptr[0]);
sizeof(arrofptr[1]);

then it will print 4

解决方案

No. Not without using extra data somewhere that stores the allocated sizes.

这篇关于如何获得的大小动态(使用malloc或释放calloc)分配的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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