sizeof 运算符的问题

发布于 2024-10-13 19:56:58 字数 827 浏览 4 评论 0原文

由于我想在函数中动态查找数组大小,因此我使用了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

献世佛 2024-10-20 19:56:58
void getSize(int *S1)

当您将数组传递给此函数时,它会衰减为指针类型,因此sizeof运算符将返回指针的大小。

但是,您将函数定义为,

template<int N>
void getSize(int (&S1)[N])
{
   //N is the size of array
   int S_size1 = N;
   int S_size2 = sizeof(S1)/sizeof(int); //would be equal to N!!
   std::cout<<"array size(in function):"<<S_size1<<std::endl;
   std::cout<<"array size(in function):"<<S_size2<<std::endl;
}

int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6};
getSize(S); //same as before

那么您可以在函数中拥有数组的大小!

在这里亲自观看演示:http://www.ideone.com/iGXNU

void getSize(int *S1)

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,

template<int N>
void getSize(int (&S1)[N])
{
   //N is the size of array
   int S_size1 = N;
   int S_size2 = sizeof(S1)/sizeof(int); //would be equal to N!!
   std::cout<<"array size(in function):"<<S_size1<<std::endl;
   std::cout<<"array size(in function):"<<S_size2<<std::endl;
}

int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6};
getSize(S); //same as before

then you can have the size of array, in the function!

See the demonstration yourself here : http://www.ideone.com/iGXNU

夏雨凉 2024-10-20 19:56:58

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). In main(), you're getting size of array.

If you want to know array size, pass result of sizeof(S) as additional argument to getSize().

More alternatives would be using some container (like std::vector) or turning function into template function, as Nawaz proposed.

金兰素衣 2024-10-20 19:56:58

S是一个int *,一个指向整数的指针,它是一个内存地址,在你的机器上是整数大小的两倍。

如果你想要数组的大小(即元素的数量),你无法直接在纯 C 中得到它。但由于这是一个 C++ 问题,所以有一种方法:使用 vector,它有一个 size() 方法。

实际上,这并不完全正确:在您声明 S 的函数内(并且仅当它在编译时显式初始化,就像您在示例中所做的那样 - 即使 < code>new int[19] 不起作用),sizeof 运算符实际上确实得到了正确的答案,这就是为什么 C++ 允许你这样做:

int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6};
vector<int> v(S, S + sizeof(S) / sizeof(int) );

然后你可以使用 < code>v.size() (请参阅这些文档) 。

Nawaz 在其他地方的模板版本是另一个很好的建议,它强制编译器携带有关 C++ 数组构造的完整信息(再次注意,这在编译时都是已知的,这就是为什么您可以明确参数中的大小)。

S is an int *, 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 a size() 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 -- even new int[19] doesn't work), the sizeof operator actually does get the correct answer, which is why c++ allows you to do this:

int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6};
vector<int> v(S, S + sizeof(S) / sizeof(int) );

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).

触ぅ动初心 2024-10-20 19:56:58

您正在获取指向数组的指针的大小。如果您想要数组的大小,则必须将元素数量乘以每个元素的大小。

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.

失去的东西太少 2024-10-20 19:56:58

您必须将数组的大小传递给函数。

由于您仅传递指向数组中第一个元素的指针,因此您的函数没有有关其实际大小的信息。

void getSize(int *S1, size_t size)
{
    int S_Size = sizeof(*S1) * size;
}

如果你仔细想想,这是多余的: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.

void getSize(int *S1, size_t size)
{
    int S_Size = sizeof(*S1) * size;
}

This is redundant though, if you think about it :D

弃爱 2024-10-20 19:56:58

为了防止这种类型的意外误用 sizeof,您可以定义一个仅适用于数组的函数:

template<class T, int N>
int array_size(T (&)[N]) {
  return N;
}

如果您在代码中使用此函数,则在应用于 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:

template<class T, int N>
int array_size(T (&)[N]) {
  return N;
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文