C++ 如果传递给函数,则确定数组的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/968001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
determine size of array if passed to function
提问by Charles Khunt
Is it possible to determine the size of an array if it was passed to another function (size isn't passed)? The array is initialized like int array[] = { XXX } ..
如果将数组传递给另一个函数(未传递大小),是否可以确定数组的大小?数组初始化为 int array[] = { XXX } ..
I understand that it's not possible to do sizeof since it will return the size of the pointer .. Reason I ask is because I need to run a for loop inside the other function where the array is passed. I tried something like:
我知道不可能做 sizeof 因为它会返回指针的大小..我问的原因是因为我需要在传递数组的另一个函数中运行一个 for 循环。我试过类似的东西:
for( int i = 0; array[i] != NULL; i++) {
........
}
But I noticed that at the near end of the array, array[i] sometimes contain garbage values like 758433 which is not a value specified in the initialization of the array..
但我注意到在数组的近端,array[i] 有时包含垃圾值,如 758433,这不是数组初始化中指定的值。
回答by Evan Teran
The other answers overlook one feature of c++. You can pass arrays by reference, and use templates:
其他答案忽略了 C++ 的一个特性。您可以通过引用传递数组,并使用模板:
template <typename T, int N>
void func(T (&a) [N]) {
for (int i = 0; i < N; ++i) a[i] = T(); // reset all elements
}
then you can do this:
那么你可以这样做:
int x[10];
func(x);
but note, this only works for arrays, not pointers.
但请注意,这仅适用于数组,而不适用于指针。
However, as other answers have noted, using std::vector
is a better choice.
但是,正如其他答案所指出的,使用std::vector
是更好的选择。
回答by Fred Larson
If it's within your control, use a STL container such as a vector or deque instead of an array.
如果它在您的控制范围内,请使用 STL 容器(例如 vector 或 deque)而不是数组。
回答by tekBlues
Nope, it's not possible.
不,这是不可能的。
One workaround: place a special value at the last value of the array so you can recognize it.
一种解决方法:在数组的最后一个值处放置一个特殊值,以便您可以识别它。
回答by Igor Krivokon
One obvious solution is to use STL. If it's not a possibility, it's better to pass array length explicitly. I'm skeptical about use the sentinel value trick, for this particular case. It works better with arrays of pointers, because NULL is a good value for a sentinel. With array of integers, it's not that easy - you need to have a "magic" sentinel value, which is not good.
一种明显的解决方案是使用 STL。如果不可能,最好显式传递数组长度。对于这种特殊情况,我对使用哨兵值技巧持怀疑态度。它适用于指针数组,因为 NULL 是哨兵的一个很好的值。对于整数数组,这并不容易 - 您需要有一个“神奇”的哨兵值,这并不好。
Side note: If your array is defined and initalized as
旁注:如果您的数组被定义并初始化为
int array[] = { X, Y, Z };
in the same scope as your loop, then
在与您的循环相同的范围内,然后
sizeof(array) will return it's real size in bytes, not the size of the pointer. You can get the array length as
sizeof(array) 将返回它的实际大小(以字节为单位),而不是指针的大小。您可以获得数组长度为
sizeof(array) / sizeof(array[0])
However, in general case, if you get array as a pointer, you can't use this trick.
但是,在一般情况下,如果将数组作为指针获取,则无法使用此技巧。
回答by Ali
You could add a terminator to your int array then step through the array manually to discover the size within the method.
您可以向 int 数组添加一个终止符,然后手动遍历数组以发现方法中的大小。
#include<iostream>
using namespace std;
int howBigIsBareArray(int arr[]){
int counter = 0;
while (arr[counter] != NULL){
counter++;
}
return counter;
}
int main(){
int a1[6] = {1,2,3,4,5,'SizeOfMyArray: 5
'};
cout << "SizeOfMyArray: " << howBigIsBareArray(a1);
}
This program prints:
该程序打印:
##代码##This is an O(n) time complexity operation which is bad. You should never be stepping through an array just to discover its size.
这是一个糟糕的 O(n) 时间复杂度操作。你永远不应该只是为了发现它的大小而单步执行一个数组。
回答by Alex Martelli
If you can't pass the size, you do need a distinguishable sentinelvalue at the end (and you need to put it there yourself -- as you've found, you can't trust C++ to do it automagically for you!). There's no way to just have the called function magically divine the size, if that's not passed in and there is no explicit, reliable sentinel in use.
如果您不能传递大小,那么最后您确实需要一个可区分的标记值(并且您需要自己将它放在那里——正如您所发现的,您不能相信 C++ 会自动为您做这件事!) . 没有办法让被调用的函数神奇地占卜大小,如果它没有传入并且没有明确的、可靠的哨兵在使用。
回答by Alan Haggai Alavi
Can you try appending a null character \0
to the array and then send it? That way, you can just check for \0 in the loop.
您可以尝试\0
在数组中附加一个空字符然后发送它吗?这样,您只需在循环中检查 \0 即可。
回答by Saturn5tony
Actually Chucks listing of
实际上 Chucks 列表
for( int i = 0; array[i] != NULL; i++) { ........ }
for( int i = 0; array[i] != NULL; i++) { ........ }
A sizeof before each call is wasteful and is needed to know what you get.
每次调用之前的 sizeof 都是浪费的,需要知道你得到了什么。
Works great if you put a NULL at the end of the arrays.
如果在数组末尾放置一个 NULL,效果会很好。
Why?? With embedded designs passing a sizeof in each routine makes each call very large compared to a NULL with each array. I have a 2K PIC16F684 chip and it takes upto 10 percent of the chip with 12 calls using a passed sizeof along with the array. With just the array and Chucks code with NULLS om each array... I get 4 percent needed.
为什么??与每个数组的 NULL 相比,嵌入式设计在每个例程中传递 sizeof 使得每次调用都非常大。我有一个 2K PIC16F684 芯片,它占用了芯片的 10%,使用传递的 sizeof 和数组进行了 12 次调用。只用数组和 Chucks 代码,每个数组都带有 NULLS ......我需要 4%。
A true case in point.. thanks chuck good call.
一个真实的例子……谢谢查克的好电话。