d*******n 发帖数: 524 | 1 我运行了下面的code
void f(int a[]) {
cout << "inside f:" << sizeof a << endl;
}
int main() {
int a[] = {2,3,4,5,6,7,8,9};
cout << "outside: " << sizeof a << endl;
f(a);
char e;
cin >> e;
return 0;
}
发现输出是:
outside: 32
inside f:4
也就是说在f内部用sizeof的话得到不是这个数组的size
那么在数组内部如何得到数组的size呢? |
t****t 发帖数: 6806 | 2 no you can not get the array size if the array is function parameter. T[]
will be automatically converted to T* for function parameters.
theoretically, the size of array is a part the type. int [5] and int [3] are
different types. therefore it is impossible to get the size information at
runtime, if you don't know it at compile time.
【在 d*******n 的大作中提到】 : 我运行了下面的code : void f(int a[]) { : cout << "inside f:" << sizeof a << endl; : } : int main() { : : int a[] = {2,3,4,5,6,7,8,9}; : cout << "outside: " << sizeof a << endl; : f(a); : char e;
|
d*******n 发帖数: 524 | 3 This reminds me another question:
should I, as a c++ programmer, always use vector rather than array?
are
at
【在 t****t 的大作中提到】 : no you can not get the array size if the array is function parameter. T[] : will be automatically converted to T* for function parameters. : theoretically, the size of array is a part the type. int [5] and int [3] are : different types. therefore it is impossible to get the size information at : runtime, if you don't know it at compile time.
|
t****t 发帖数: 6806 | 4 you don't have to.
【在 d*******n 的大作中提到】 : This reminds me another question: : should I, as a c++ programmer, always use vector rather than array? : : are : at
|
S**I 发帖数: 15689 | 5 貌似大多数的C++教程都是建议能用vector就用vector,不用array
【在 d*******n 的大作中提到】 : This reminds me another question: : should I, as a c++ programmer, always use vector rather than array? : : are : at
|
i******t 发帖数: 370 | 6 That's because they are "C++" tutorials instead of "C"
【在 S**I 的大作中提到】 : 貌似大多数的C++教程都是建议能用vector就用vector,不用array
|
S**I 发帖数: 15689 | 7 说的不就是C++嘛,C里也没有vector这东东,不用array用啥?
【在 i******t 的大作中提到】 : That's because they are "C++" tutorials instead of "C"
|
d*****1 发帖数: 1837 | 8 performance issue, there is overhead with vector. |
v*s 发帖数: 946 | 9 如果对performance要求不高,怎么方便怎么用吧。
vector咋啦? |
l******e 发帖数: 12192 | 10 not true
【在 d*****1 的大作中提到】 : performance issue, there is overhead with vector.
|
s*****u 发帖数: 164 | 11 Thinking in C++, Vol. II, Page. 249 |
f******y 发帖数: 2971 | 12 vector是在heap上的,创建时稍慢一些。另外,vector也比实际数组大一些。但这些差
别应该都不重要,vector的好处远远多于它的overhead。 |