我如何返回我使用malloc分配指针的大小? [英] How I return the size of the pointer that I have allocate with malloc?

查看:482
本文介绍了我如何返回我使用malloc分配指针的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅本示例中!



int main( int argc, char ** argv )
{
    int *ptr = malloc(100 * sizeof (int));

    printf("sizeof(array) is %d bytes\n", sizeof(ptr));
}

printf函数返回仅4个字节!
什么是错的?

The printf function return only 4 bytes! What is wrong?

太感谢了!

推荐答案

没有什么是错的。你所要求的,并得到,指针的大小,你的平台上。

Nothing is wrong. You are asking for, and getting, the size of the pointer on your platform.

这不是一般的可能获得,在一个指针点,你必须自己记住,如果你以后需要的内存块的大小。

It is not in general possible to get the size of the memory block that a pointer points at, you must remember it yourself if you need it later.

这篇关于我如何返回我使用malloc分配指针的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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