数组C++ [duplicate]的大小

时间:2021-12-23 21:44:46

Possible Duplicate:
Sizeof array passed as parameter

可能重复:作为参数传递的Sizeof数组

I was wondering why the output of the following code is 1 and 9. Is that because of undeclared array in function size? How can I separate "size of array" to a function?

我想知道为什么下面代码的输出是1和9。这是因为函数大小的未声明数组吗?如何将“数组大小”分离到函数中?

#include "stdafx.h"
#include <iostream>
using namespace std;

int size(int a[])
{
    return sizeof a/sizeof a[0];
}

int main()
{   
    int a[] = {5,2,4,7,1,8,9,10,6};
    cout << size(a) << endl;
    cout << sizeof a/sizeof a[0] << endl;
    system("pause");
    return 0;
}

7 个解决方案

#1


12  

When you write size(a) then you're passing a pointer and not an array. Since the size of a pointer and an int is 4 or 8 (depending on ABI), you get sizeof(int *)/sizeof int (4/4=1 for 32-bit machines and 8/4=2 for 64-bit ones) which is 1 or 2.

当您写入size(a)时,您传递的是一个指针,而不是一个数组。由于指针和int的大小是4或8(取决于ABI),所以可以得到sizeof(int *)/sizeof int(32位机器的4/4=1,64位机器的8/4=2)也就是1或2。

In C++ when pass an array as an argument to a function, actually you're passing a pointer to an array.

在c++中,当将数组作为参数传递给函数时,实际上是将指针传递给数组。

#2


7  

Maroun85 answer is correct. This is not obvious but a in int size(int a[]) is a pointer.

Maroun85回答是正确的。这并不明显,但是int大小(int a[])是一个指针。

But why don't you do it the c++ way. Using std::vectors

但是为什么不使用c++呢?使用std::向量

std::vector<int> a = {5,2,4,7,1,8,9,10,6};
cout << a.size() << endl;

no tricks here

这里没有技巧

-- edit

——编辑

If your compiler does not support c++11. You can do:

如果编译器不支持c++11。你能做什么:

std::vector<int> a;
a.push(5);
a.push(2);
...
cout << a.size() << endl;

#3


5  

Your size() function cannot work like you want it to because when you pass an array to a function, the array decays to a pointer to its first element. Your a decays to a int* and sizeof operator on a pointer returns the size of the pointer, not the array. Consider using a std::vector<int> instead as this will allow you to retrieve the size of the vector each time you pass it to the function.

您的size()函数不能像您希望的那样工作,因为当您将数组传递给函数时,数组会衰减到指向第一个元素的指针。指针上的a衰减为int*和sizeof运算符,返回的是指针的大小,而不是数组。考虑使用std::vector ,因为这将允许您在每次将其传递给函数时检索向量的大小。

#4


4  

When passing arrays to functions, they decay to pointers. So, your size() function is equivalent to:

当将数组传递给函数时,它们会衰减到指针。因此,您的size()函数相当于:

int size(int* a)
{
    return sizeof a/sizeof a[0];
}

And sizeof a is the size of a pointer, which is the same as the size of an int here, hence the output.

sizeof a是一个指针的大小,它和这里的int的大小一样,因此输出。

#5


3  

sizeof is evaluated at compile time, not at runtime. The compiler does not analyse what you pass to function size, but rather treats the function parameter as a pointer. Thus in your function size the result of sizeof a is the size of a pointer to an int, which is, by chance, equal to the size of an int on your system.

sizeof是在编译时评估的,而不是在运行时。编译器不分析传递给函数大小的内容,而是把函数参数当作指针。因此,在函数大小中,sizeof a的结果是指向int的指针的大小,碰巧,它等于系统上int的大小。

#6


3  

Remember that array are always passed by pointer.

记住数组总是由指针传递的。

So in the function a is a pointer to int, and (for 32bit-intergers) the size of a pointer to int is the same of the size of an int.

所以在函数a中是指向int的指针,(对于32位交错)指向int的指针的大小与int的大小相同。

#7


3  

The best explanation why your solution does not work is in Maroun's answer.

最好的解释就是为什么你的解决方案行不通。

About the second part of the question ("how can it be done?"), you can do this with a template function:

关于问题的第二部分(“如何做到这一点?”),您可以使用模板函数:

template <typename T, size_t n> const size_t size(const T (&)[n]) { return n; }

Of course, this works only if the size of the array is constant (constant, as seen by the compiler), but it can only ever work in this case anyway -- an array does not store its size anywhere, so if it's not a known compile-time constant, there is no way to know it.

当然,这只能如果数组的大小恒定(常数,在编译器),但它只能工作在这种情况下,数组不存储它的大小,如果这不是一个已知的编译时常量,没有办法知道。

If you need this to work with arrays that are not compile-time constants (say, something you allocate with operator new[], or using a non-standard compiler extension), you need to explicitly store the size somewhere.

如果您需要它来处理非编译时常量的数组(例如,您使用操作符new[]分配的数组,或者使用非标准的编译器扩展),您需要显式地将大小存储在某处。

(Incidentially, my above statement is technically wrong, indeed the allocation size is usually stored, but this is an implementation detail which you cannot and should not depend on.)

(碰巧,我上面的语句在技术上是错误的,实际上分配大小通常是存储的,但是这是一个实现细节,您不能也不应该依赖它)。

#1


12  

When you write size(a) then you're passing a pointer and not an array. Since the size of a pointer and an int is 4 or 8 (depending on ABI), you get sizeof(int *)/sizeof int (4/4=1 for 32-bit machines and 8/4=2 for 64-bit ones) which is 1 or 2.

当您写入size(a)时,您传递的是一个指针,而不是一个数组。由于指针和int的大小是4或8(取决于ABI),所以可以得到sizeof(int *)/sizeof int(32位机器的4/4=1,64位机器的8/4=2)也就是1或2。

In C++ when pass an array as an argument to a function, actually you're passing a pointer to an array.

在c++中,当将数组作为参数传递给函数时,实际上是将指针传递给数组。

#2


7  

Maroun85 answer is correct. This is not obvious but a in int size(int a[]) is a pointer.

Maroun85回答是正确的。这并不明显,但是int大小(int a[])是一个指针。

But why don't you do it the c++ way. Using std::vectors

但是为什么不使用c++呢?使用std::向量

std::vector<int> a = {5,2,4,7,1,8,9,10,6};
cout << a.size() << endl;

no tricks here

这里没有技巧

-- edit

——编辑

If your compiler does not support c++11. You can do:

如果编译器不支持c++11。你能做什么:

std::vector<int> a;
a.push(5);
a.push(2);
...
cout << a.size() << endl;

#3


5  

Your size() function cannot work like you want it to because when you pass an array to a function, the array decays to a pointer to its first element. Your a decays to a int* and sizeof operator on a pointer returns the size of the pointer, not the array. Consider using a std::vector<int> instead as this will allow you to retrieve the size of the vector each time you pass it to the function.

您的size()函数不能像您希望的那样工作,因为当您将数组传递给函数时,数组会衰减到指向第一个元素的指针。指针上的a衰减为int*和sizeof运算符,返回的是指针的大小,而不是数组。考虑使用std::vector ,因为这将允许您在每次将其传递给函数时检索向量的大小。

#4


4  

When passing arrays to functions, they decay to pointers. So, your size() function is equivalent to:

当将数组传递给函数时,它们会衰减到指针。因此,您的size()函数相当于:

int size(int* a)
{
    return sizeof a/sizeof a[0];
}

And sizeof a is the size of a pointer, which is the same as the size of an int here, hence the output.

sizeof a是一个指针的大小,它和这里的int的大小一样,因此输出。

#5


3  

sizeof is evaluated at compile time, not at runtime. The compiler does not analyse what you pass to function size, but rather treats the function parameter as a pointer. Thus in your function size the result of sizeof a is the size of a pointer to an int, which is, by chance, equal to the size of an int on your system.

sizeof是在编译时评估的,而不是在运行时。编译器不分析传递给函数大小的内容,而是把函数参数当作指针。因此,在函数大小中,sizeof a的结果是指向int的指针的大小,碰巧,它等于系统上int的大小。

#6


3  

Remember that array are always passed by pointer.

记住数组总是由指针传递的。

So in the function a is a pointer to int, and (for 32bit-intergers) the size of a pointer to int is the same of the size of an int.

所以在函数a中是指向int的指针,(对于32位交错)指向int的指针的大小与int的大小相同。

#7


3  

The best explanation why your solution does not work is in Maroun's answer.

最好的解释就是为什么你的解决方案行不通。

About the second part of the question ("how can it be done?"), you can do this with a template function:

关于问题的第二部分(“如何做到这一点?”),您可以使用模板函数:

template <typename T, size_t n> const size_t size(const T (&)[n]) { return n; }

Of course, this works only if the size of the array is constant (constant, as seen by the compiler), but it can only ever work in this case anyway -- an array does not store its size anywhere, so if it's not a known compile-time constant, there is no way to know it.

当然,这只能如果数组的大小恒定(常数,在编译器),但它只能工作在这种情况下,数组不存储它的大小,如果这不是一个已知的编译时常量,没有办法知道。

If you need this to work with arrays that are not compile-time constants (say, something you allocate with operator new[], or using a non-standard compiler extension), you need to explicitly store the size somewhere.

如果您需要它来处理非编译时常量的数组(例如,您使用操作符new[]分配的数组,或者使用非标准的编译器扩展),您需要显式地将大小存储在某处。

(Incidentially, my above statement is technically wrong, indeed the allocation size is usually stored, but this is an implementation detail which you cannot and should not depend on.)

(碰巧,我上面的语句在技术上是错误的,实际上分配大小通常是存储的,但是这是一个实现细节,您不能也不应该依赖它)。