How to get the size of dynamically (using malloc or calloc )allocated memory? [duplicate]

梦想的初衷 提交于 2019-12-02 05:01:49

问题


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


回答1:


No, there is no way to find how much memory a pointer is referring to.

At least not on any system, so no portable way.




回答2:


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



来源:https://stackoverflow.com/questions/12803562/how-to-get-the-size-of-dynamically-using-malloc-or-calloc-allocated-memory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!