简体   繁体   中英

Finding the size of int [] array

In the following function, how can we find the length of the array

int fnLenghthOfArray(int arry[]){
    return sizeof(arry)/sizeof(int); // This always returns 1
}

Here this function always returns 1. Where as, sizeof(arry)/sizeof(int) gives the actual length of the array, in the function where it is declared.

If we use vector or template like

template<typename T,int N> 

int fnLenghthOfArray(T (&arry)[N]){

}

we can get the size. But here I am not allowed to change the function prototype.

Please help me to find this.

Remember, in C when you pass an array as an argument to a function, you're passing a pointer to the array. If you want to pass the size of the array, you should pass it as a separated argument.

The size of a pointer and an int is 4 or 8 or something else - depending on ABI .
In your case, it's 4 , so you're getting sizeof(int *)/sizeof int which is 1.


Here is a useful trick

You can store the length of the array in the first element of it:

int myArray[]= {-1, 1, 2, 3, 4, 5};
myArray[0] = sizeof(myArray) / sizeof(myArray[0]) - 1;
//The -1 because.. the first element is only to indicate the size

Now, myArray[0] will contain the size of the array.

In function decalration, array is a pointer:

int fnLenghthOfArray(int arry[])
                        ^
                          is same as int* array 

And in your system sizeof(int*) == sizeof(int) .

You function declaration

int fnLenghthOfArray(int arry[]);

is equivalent to

int fnLenghthOfArray(int* arry);

hence your calculation yields 1 (based on the assumption that the size of a pointer to int and size of an int are the same).

Your only option to get the size of the array is to provide an additional parameter

int fnLenghthOfArray(int arry[], std::size_t size);

Alternatively you could use one of the C++ containers like vector or array

int fnLenghthOfArray(int arry[]){
    return sizeof(arry)/sizeof(int); // This always returns 1
}

This function returns 1 because is performing a division between the size of a pointer and the size of an integer. In most architectures, the size of a pointer is equal to the size of an integer. For instance, in the x86 architecture both have size 4 bytes.

Where as, sizeof(arry)/sizeof(int) gives the actual length of the array, in the function where it is declared

Because in this case the compiler knows that arry is an array and its size. Whereas, in the previous function, the compiler knows arry only as a pointer. In fact, when you specify the function prototype, there is not difference between int arry[] and int * arry .

int arry[]

is equivalent to

int *arry

and the sizeof() operator returns 4 when applied to arry because it's the size of a pointer (or reference in the case of arry[]), the size of the int is also 4 bytes and that's why it always returns 1.

To solve your problem you must implement the array in a different way. Maybe the first element should always have the size of the array. Otherwise you could use the vector class from STL or list .

You can't get size of array in C or C++ .

Array in this languages is simply pointer to first element. You need to keep size of array by yourself.

Here is code snippet using Maroun's trick.

#include<stdio.h>

void print_array(int *array);
void shift_array_normal(int *array,int arrayLen);

int main(void)
{
    int array[]= {-1,32,44,185,28,256,22,50};
    array[0] = sizeof(array) / sizeof(array[0]) - 1;

    print_array(array);
    return 0;
}

void print_array(int *array){
 int index,arrayLen = array[0];

 //length of array is stored in arrayLen now we can convert array back.
 printf("Length of array is : %d\n",arrayLen);

 //convert array back to normal.
 shift_array_normal(array,arrayLen);

 //print int array .
 for(index = 0; index < arrayLen; index++)
    printf("array[%d] = %d\n",index,array[index]);
}

/*removing length element from array and converting it back to normal array*/
void shift_array_normal(int *array,int arrayLen){
    int index;

    for(index = 0; index < arrayLen; index++)
     array[index] = array[index + 1];       
}
#include<iostream>

int main()
{
    int array[300];

    int d = sizeof(array)/4;
    std::cout<<d;   
} 

Use:

// sizeof(array)/4  for "int" array reserves 4 bits.
// sizeof(array)/4  for "float" array reserves 4 bits.
// sizeof(array)    for "char" array reserves 2 bits.
// sizeof(array)    for "bool" array reserves 2 bits.
// sizeof(array)/8  for "double" array reserves 8 bits.
// sizeof(array)/16 for "long double" array reserves 16 bits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM