determine size of array if passed to function

故事扮演 提交于 2019-12-17 02:22:12

问题


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

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:

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


回答1:


The other answers overlook one feature of c++. You can pass arrays by reference, and use templates:

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.




回答2:


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




回答3:


Nope, it's not possible.

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




回答4:


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.

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[0])

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




回答5:


You could add a terminator to your int array then step through the array manually to discover the size within the method.

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




回答6:


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.




回答7:


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.




回答8:


Actually Chucks listing of

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

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.

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.

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



来源:https://stackoverflow.com/questions/968001/determine-size-of-array-if-passed-to-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!