如果传递给函数,则确定数组的大小

时间:2022-03-08 21:36:49

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

如果数组被传递给另一个函数(size未传递),是否可能确定数组的大小?数组初始化为int数组[]= {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..

但是我注意到在数组的近端,数组[I]有时包含一些垃圾值,比如758433,这不是数组初始化中指定的值。

8 个解决方案

#1


44  

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是更好的选择。

#2


13  

If it's within your control, use a STL container such as a vector or deque instead of an array.

如果它在您的控制范围内,请使用STL容器,如向量或deque,而不是数组。

#3


8  

Nope, it's not possible.

不,这是不可能的。

One workaround: place a special value at the last value of the array so you can recognize it.

一种变通方法:在数组的最后一个值处放置一个特殊值,以便您可以识别它。

#4


6  

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(数组)将返回它的实际大小,以字节为单位,而不是指针的大小。可以得到数组的长度为

sizeof(array) / sizeof(array[0])

However, in general case, if you get array as a pointer, you can't use this trick.

但是,在一般情况下,如果将数组作为指针,就不能使用这个技巧。

#5


3  

If you can't pass the size, you do need a distinguishable sentinel value 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.

如果您不能传递大小,那么在最后需要一个可识别的sentinel值(并且您需要自己将它放在那里——正如您所发现的,您不能信任c++自动地为您传递它!)如果没有被传递进来,并且没有明确的、可靠的标记,那么就没有办法神奇地将这个功能称为神圣的大小。

#6


3  

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,'\0'};
    cout << "SizeOfMyArray: " << howBigIsBareArray(a1);
}

This program prints:

这个程序打印:

SizeOfMyArray: 5

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)时间复杂度的操作,不好。您不应该只是为了发现数组的大小而对数组进行步进。

#7


1  

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。

#8


0  

Actually Chucks listing of

其实放弃的清单

for( int i = 0; array[i] != NULL; i++) { ........ }

for(int i = 0;array[我]! =零;我+ +){ ........}

A sizeof before each call is wasteful and is needed to know what you get.

每次通话前的尺码是浪费的,需要知道你得到了什么。

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.

为什么? ?通过在每个例程中传递sizeof的嵌入式设计,使每个调用都比每个数组的NULL大。我有一个2K的PIC16F684芯片,它占据了芯片的10%,使用一个传入的sizeof和数组进行12次调用。只使用数组,并使用NULLS om(每个数组)我需要4%

A true case in point.. thanks chuck good call.

这是一个很好的例子。由于卡盘好电话。

#1


44  

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是更好的选择。

#2


13  

If it's within your control, use a STL container such as a vector or deque instead of an array.

如果它在您的控制范围内,请使用STL容器,如向量或deque,而不是数组。

#3


8  

Nope, it's not possible.

不,这是不可能的。

One workaround: place a special value at the last value of the array so you can recognize it.

一种变通方法:在数组的最后一个值处放置一个特殊值,以便您可以识别它。

#4


6  

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(数组)将返回它的实际大小,以字节为单位,而不是指针的大小。可以得到数组的长度为

sizeof(array) / sizeof(array[0])

However, in general case, if you get array as a pointer, you can't use this trick.

但是,在一般情况下,如果将数组作为指针,就不能使用这个技巧。

#5


3  

If you can't pass the size, you do need a distinguishable sentinel value 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.

如果您不能传递大小,那么在最后需要一个可识别的sentinel值(并且您需要自己将它放在那里——正如您所发现的,您不能信任c++自动地为您传递它!)如果没有被传递进来,并且没有明确的、可靠的标记,那么就没有办法神奇地将这个功能称为神圣的大小。

#6


3  

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,'\0'};
    cout << "SizeOfMyArray: " << howBigIsBareArray(a1);
}

This program prints:

这个程序打印:

SizeOfMyArray: 5

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)时间复杂度的操作,不好。您不应该只是为了发现数组的大小而对数组进行步进。

#7


1  

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。

#8


0  

Actually Chucks listing of

其实放弃的清单

for( int i = 0; array[i] != NULL; i++) { ........ }

for(int i = 0;array[我]! =零;我+ +){ ........}

A sizeof before each call is wasteful and is needed to know what you get.

每次通话前的尺码是浪费的,需要知道你得到了什么。

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.

为什么? ?通过在每个例程中传递sizeof的嵌入式设计,使每个调用都比每个数组的NULL大。我有一个2K的PIC16F684芯片,它占据了芯片的10%,使用一个传入的sizeof和数组进行12次调用。只使用数组,并使用NULLS om(每个数组)我需要4%

A true case in point.. thanks chuck good call.

这是一个很好的例子。由于卡盘好电话。