C语言 在 C 中查找 malloc() 数组长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14004704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find malloc() array length in C?
提问by GCBenson
Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)
可能的重复:
如何找到 sizeof(指向数组的指针)
I'm learning how to create a dynamic array in C, but have come across an issue I can't figure out.
我正在学习如何在 C 中创建一个动态数组,但遇到了一个我无法弄清楚的问题。
If I use the code:
如果我使用代码:
int num[10];
for (int i = 0; i < 10; i++) {
num[i] = i;
}
printf("sizeof num = %li\n sizeof num[0] = %li", sizeof(num), sizeof(num[0]));
I get the output:
我得到输出:
sizeof num = 40
sizeof num[0] = 4
This is what I'd expect to happen. However if I malloc the size of the array like:
这是我期望发生的事情。但是,如果我分配数组的大小,例如:
int *num;
num = malloc(10 * sizeof(int));
for (int i = 0; i < 10; i++) {
num[i] = i;
}
printf("sizeof num = %li\n sizeof num[0] = %li", sizeof(num), sizeof(num[0]));
Then I get the output:
然后我得到输出:
sizeof num = 8
sizeof num[0] = 4
I'm curious to know why the size of the array is 40 when I use the fixed length method, but not when I use malloc().
我很想知道为什么当我使用定长方法时数组的大小是 40,而当我使用malloc().
回答by effeffe
In the second case, numis not an array, is a pointer. sizeofis giving you the size of the pointer, which seems to be 8 bytes on your platform.
在第二种情况下,num不是数组,是指针。sizeof正在为您提供指针的大小,它在您的平台上似乎是 8 个字节。
There is no way to know the size of a dynamically allocated array, you have to save it somewhere else. sizeoflooks 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 mallocin any way, and sizeofargument can't be applied to an incomplete type, like int[].
无法知道动态分配数组的大小,您必须将其保存在其他地方。sizeof查看类型,但您无法以任何方式int[5]从结果中获得完整的数组类型(具有指定大小的数组类型,如 type )malloc,并且sizeof参数不能应用于不完整的类型,如int[].
回答by Luchian Grigore
Arrays are not pointers (the decay to pointers in some situations, not here).
数组不是指针(在某些情况下会衰减到指针,而不是这里)。
The first one is an array - so sizeofgives you the size of the array = 40 bytes.
第一个是一个数组 - 所以sizeof给你数组的大小 = 40 字节。
The second is a pointer (irrespective of how many elements it points to) - sizeofgives you sizeof(int*).
第二个是一个指针(不管它指向多少个元素) -sizeof给你sizeof(int*).
回答by Rubens
The second size refers to the size of a pointer, that, in your machine -- probably 64bits --, is 8 bytes.
第二个大小是指指针的大小,在您的机器中——可能是 64 位——是 8 个字节。
You cannot use sizeof()to recover the size of a dynamically allocated structure, but you can do so for statically allocated ones.
您不能使用sizeof()来恢复动态分配结构的大小,但可以对静态分配的结构进行恢复。
回答by Mats Petersson
If you want to know the size of something you have allocated, then you need to "remember" that yourself, since your code did the allocation. If your code hasn't done the allocation, then there's no way [in a standard sense] to find out how large the memory are a pointer is pointing to. You just have to "know" some other way.
如果你想知道你分配的东西的大小,那么你需要自己“记住”,因为你的代码做了分配。如果您的代码还没有完成分配,那么 [在标准意义上] 没有办法找出指针指向的内存有多大。你只需要以其他方式“知道”。

