为什么函数不知道数组大小?
如果我写的话
int main()
{
int a[100] = {1,2,3,4,};
cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array,
//isn't it
return 0;
}
我会得到400!
如果我写
void func(int *a);
int main()
{
int a[100] = {1,2,3,4,};
func(a);
return 0;
}
void func(int *a)
{
cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array
}
然后我得到1!
那么为什么函数不知道数组大小呢?
If I write
int main()
{
int a[100] = {1,2,3,4,};
cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array,
//isn't it
return 0;
}
I get 400!
If I write
void func(int *a);
int main()
{
int a[100] = {1,2,3,4,};
func(a);
return 0;
}
void func(int *a)
{
cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array
}
Then I get 1!
So why function does not know the array size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
当传递给函数时,数组会衰减为指针,因此您得到的只是指针的大小。
Arrays decay to pointers when passed to functions, so all you will get is the size of the pointer.
sizeof
返回类型的大小。在第二个示例中,func( int *a )
,a 是一个指针,sizeof
将这样报告它。即使您执行了 func( int a[100] ) , a 也将是一个指针。如果您想要 func 中数组的大小,则必须将其作为额外参数传递。sizeof
returns the size of the type. In the second example,func( int *a )
, a is a pointer andsizeof
will report it as such. Even if you didfunc( int a[100] )
, a would be a pointer. If you want the size of the array in func, you must pass it as an extra argument.这不起作用,因为 sizeof 是在编译时计算的。该函数没有关于其参数大小的信息(它只知道它指向一个内存地址)。
考虑使用 STL 向量,或者将数组大小作为参数传递给函数。
Marcel Guzman 在计算数组的大小中回答了这个问题!
This isn't working because sizeof is calculated at compile-time. The function has no information about the size of its parameter (it only knows that it points to a memory address).
Consider using an STL vector instead, or passing in array sizes as parameters to functions.
This was answered by Marcel Guzman in Calculating size of an array!
当将数组作为参数传递给采用指针的函数时,数组将作为指针衰减。
因此,也许解决方案是提供一个正确的函数,将对数组的引用作为参数:
当然,该函数必须是模板化的。
When passing your array as a parameter to a function taking a pointer, the array decays as a pointer.
So perhaps the solution is to provide a correct function taking a reference to an array as a parameter :
Of course, the function must be templated.
不,你错了。
如果我运行你的第二部分代码,它会在我的计算机上给出 1。这不是 400。
产生
No. You are wrong.
If I run your second part of code, it gives 1 on my computer. It's not 400.
Produces
第一次你得到 400,因为你只传递 sizeof(a),而不是 sizeof(a)/sizeof(a[0]) 到 cout。您需要用括号将该计算括起来以获得正确的输出值,即:
第二次,您应该得到 2、4 或 8(取决于体系结构),绝对不是 400,因为您实际上是在输出
:通用指针的大小始终是固定值。
You get 400 the first time because you are passing only sizeof(a), not sizeof(a)/sizeof(a[0]), to cout. You need to wrap that calculation with parenthesis to get the correct value outputted, ie:
For the second time, you should be getting 2, 4, or 8 (depending on architecture), definately not 400, since you are essentially outputting this:
Where the size of a generic pointer is always a fixed value.
指针就是指针。这意味着,它只是指向内存,仅此而已。创建一个指向数组的指针(通常意味着指向数组第一个元素的指针,但不一定)仍然只是指向某个内存位置的指针。由于内存地址只是一个内存地址,因此指针也无法知道它指向的内存最初是一个数组,或者该数组有多长。
这就是指针的工作原理。它们指向记忆,仅此而已。
A pointer is a pointer. That means, it simply points to memory, and that's all about it. Creating a pointer to an array (which usually means a pointer to the first element of the array, but not necessarily) is still only a pointer to some memory location. As a memory address is simply a memory address there is also no way for the pointer to know that the memory it is pointing to originally was an array, or how long that array was.
It's simply how pointers work. They point to memory, and that's all.
该函数不知道示例中的数组大小,因为您采取了显式步骤将数组转换为指针类型,从而完全剥离了函数参数的原始数组性质。您自己再一次采取了故意的步骤来确保该函数不知道数组的大小。在这种情况下,看到你问为什么函数不知道数组大小的问题是相当奇怪的。
如果您希望函数以数组形式接收参数,则必须将其作为数组而不是单纯的指针传递,并相应地声明相应的函数参数。 C++ 中的数组不能“按值”传递,这意味着您必须“按引用”传递它,作为一种可能性
The function does not know the array size in your example because you took explicit steps to convert your array to pointer type, thus completely stripping the function parameter of its original array nature. Once again, you yourself took deliberate steps to make sure that the function does not know the size of the array. Under these circumstances, it is rather strange to see you ask the question about why the function doesn't know the array size.
If you what the function to receive its argument as an array, you have to pass it as an array, not as a mere pointer, and declare the corresponding function parameter accordingly. Arrays in C++ cannot be passed "by value", which means that you'll have to pass it "by reference", as one possibility