且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C中找到malloc()数组长度?

更新时间:2023-02-26 18:40:00

在第二种情况下,num不是数组,而是指针. sizeof为您提供了指针的大小,在您的平台上似乎是8个字节.

In the second case, num is not an array, is a pointer. sizeof is giving you the size of the pointer, which seems to be 8 bytes on your platform.

无法知道动态分配的数组的大小,必须将其保存在其他位置. sizeof查看类型,但是您不能以任何方式从malloc的结果中获得完整的数组类型(具有指定大小的数组类型,如类型int[5]),并且sizeof参数可以不适用于int[]之类的不完整类型.

There is no way to know the size of a dynamically allocated array, you have to save it somewhere else. sizeof looks at the type, but you can't obtain a complete array type (array type with a specified size, like the type int[5]) from the result of malloc in any way, and sizeof argument can't be applied to an incomplete type, like int[].