sizeof 运算符的问题
由于我想在函数中动态查找数组大小,因此我使用了 sizeof 运算符。但我得到了一些意想不到的结果。 这是一个演示程序,向您展示我想要做什么。
//------------------------------------------------------------------------------------------
#include <iostream>
void getSize(int *S1){
int S_size = sizeof S1/sizeof(int);
std::cout<<"array size(in function):"<<S_size<<std::endl;
}
int main(){
int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6};
getSize(S);
std::cout<<"array size:"<<sizeof S/sizeof(int)<<std::endl;
return 0;
}
//------------------------------------------------------------------------------------------
编译命令: g++ demo1.cc -o demo1 {fedora 12}
输出:
array size(in function):2
array size:19
请解释一下,为什么会发生这种情况。 可以采取什么措施来解决这个问题。
As i want to find array size dynamically in function, i used sizeof operator. But i got some unexpected result.
here is one demo program to show you, what i want to do.
//------------------------------------------------------------------------------------------
#include <iostream>
void getSize(int *S1){
int S_size = sizeof S1/sizeof(int);
std::cout<<"array size(in function):"<<S_size<<std::endl;
}
int main(){
int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6};
getSize(S);
std::cout<<"array size:"<<sizeof S/sizeof(int)<<std::endl;
return 0;
}
//------------------------------------------------------------------------------------------
compilation command : g++ demo1.cc -o demo1 {fedora 12}
output:
array size(in function):2
array size:19
please explain ,why this is happening.
what can be done to solve this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您将数组传递给此函数时,它会衰减为指针类型,因此
sizeof
运算符将返回指针的大小。但是,您将函数定义为,
那么您可以在函数中拥有数组的大小!
在这里亲自观看演示:http://www.ideone.com/iGXNU
When you pass an array to this function, it decays to pointer type, so
sizeof
operator will return the size of pointer.However, you define your function as,
then you can have the size of array, in the function!
See the demonstration yourself here : http://www.ideone.com/iGXNU
在
getSize()
中,您将获取指针的大小,即 8 个字节(因为您可能正在运行 64 位操作系统)。在main()
中,您将获取数组的大小。如果您想知道数组大小,请将
sizeof(S)
的结果作为附加参数传递给getSize()
。更多的替代方案是使用一些容器(如 std::vector )或将函数转换为模板函数,正如 Nawaz 所提议的那样。
Inside
getSize()
, you're getting size of pointer, which is 8 bytes (since you're probably running 64-bit OS). Inmain()
, you're getting size of array.If you want to know array size, pass result of
sizeof(S)
as additional argument togetSize()
.More alternatives would be using some container (like
std::vector
) or turning function into template function, as Nawaz proposed.S
是一个int *
,一个指向整数的指针,它是一个内存地址,在你的机器上是整数大小的两倍。如果你想要数组的大小(即元素的数量),你无法直接在纯 C 中得到它。但由于这是一个 C++ 问题,所以有一种方法:使用
vector
,它有一个size()
方法。实际上,这并不完全正确:在您声明
S
的函数内(并且仅当它在编译时显式初始化,就像您在示例中所做的那样 - 即使 < code>new int[19] 不起作用),sizeof
运算符实际上确实得到了正确的答案,这就是为什么 C++ 允许你这样做:然后你可以使用 < code>v.size() (请参阅这些文档) 。
Nawaz 在其他地方的模板版本是另一个很好的建议,它强制编译器携带有关 C++ 数组构造的完整信息(再次注意,这在编译时都是已知的,这就是为什么您可以明确参数中的大小)。
S
is anint *
, a pointer to an integer, which is a memory address, which is on your machine twice the size of an integer.If you want the size of the array (I.e., the number of elements), you can't get that directly in pure C. But since this is a c++ question, there is a way: use a
vector
, which has asize()
method.Actually, this isn't quite true: within the function that you declare
S
(and only if it's explicitly initialized at compile time as you do in your example -- evennew int[19]
doesn't work), thesizeof
operator actually does get the correct answer, which is why c++ allows you to do this:and then you can use
v.size()
(see these docs).The template version by Nawaz elsewhere is another excellent suggestion which forces the compiler into carrying the full information about the construction of the c++ array around (again, note that this is all known at compile time, which is why you can be explicit about the size in the argument).
您正在获取指向数组的指针的大小。如果您想要数组的大小,则必须将元素数量乘以每个元素的大小。
you are getting the size of the pointer to the array. If you want the size of the array you have to multiply the number of elements by the size of each element.
您必须将数组的大小传递给函数。
由于您仅传递指向数组中第一个元素的指针,因此您的函数没有有关其实际大小的信息。
如果你仔细想想,这是多余的:D
You will have to pass the size of the array to the function.
Since you are only passing a pointer to the first element in the array, your function has no information on its actual size.
This is redundant though, if you think about it :D
为了防止这种类型的意外误用 sizeof,您可以定义一个仅适用于数组的函数:
如果您在代码中使用此函数,则在应用于 S1 时您会看到编译器错误,因为它不是数组。另外,它比 sizeof array / sizeof array[0] 更短且更明确(使用第一项的大小意味着您不必重复数组类型)。
这也已经以更通用的形式存在于 Boost 中(接受带有 size 方法的任何内容,例如 std::vector)。
To prevent this type of accidental misuse of sizeof, you can define a function which only works on arrays:
If you use this in your code, you'll see a compiler error when applied to S1, as it is not an array. Plus, it's shorter and a bit more explicit than sizeof array / sizeof array[0] (using the size of the first item means you don't have to repeat the array type).
This also already exists in Boost in a more general form (accepting anything with a size method, such as std::vector).