0

I had the online coding interview today and I really struggled while trying to calculate the size of the array. Could you please help me with how can I measure the sizeof array here? I tried my best but no luck please help here.

#include<iostream>
#include<map>
#include<vector>

using namespace std;

void arraysize(int* a) {
    cout << "size1: "<<sizeof(a) << endl;
    cout << "size2: " << sizeof(a[0]) << endl;;
}

int main()
{
    int array1[] = { 1,2,3,4,5,6,7,8 }; 

    arraysize(array1);


    return 0;

}

Result: size1: 4 size2: 4

  • Are you allowed to change `arraysize`'s signature? If so, if you make a template-function that accepts `std::array` then it's straightforward. – Dai Apr 23 '20 at 02:20
  • The function `arraysize` cannot determine the size of `array1` from the information available to it in the code shown. What was the specific question you were asked? If it was how to change the code to make the array size available, then there are several alternatives. – Eric Postpischil Apr 23 '20 at 02:20
  • Helpful reading: [What is array to pointer decay?](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) – user4581301 Apr 23 '20 at 03:20
  • Something to read https://books.google.co.nz/books?id=jXt0CwAAQBAJ&q=%22This%20cretinous%20barfbag%20uses%20sizeof%20to%20return%20the%20size%20of%20the%20array%22#v=snippet&q=%22This%20cretinous%20barfbag%20uses%20sizeof%20to%20return%20the%20size%20of%20the%20array%22&f=false – Jerry Jeremiah Apr 23 '20 at 03:26
  • Does this answer your question? [determine size of array if passed to function](https://stackoverflow.com/questions/968001/determine-size-of-array-if-passed-to-function) – ChrisMM Apr 23 '20 at 12:21

5 Answers5

3

In most cases, when you pass an array to a function, the array will be converted to a pointer. This is called an array-to-pointer decay. Once this decay happens, you lose the size information of the array. That is, you can no longer tell the size of the original array from the pointer.

However, one case in which this conversion / decay will not happen is when we pass a reference to the array. We can take advantage of this property to get the size of an array.

#include<iostream>

template<typename T, size_t N>
size_t asize(T (&array)[N])
{
    return N;
}

int main()
{
    int array1[] = { 1,2,3,4,5,6,7,8 };
    std::cout << asize(array1) << std::endl;  // 8
    return 0;
}

In the above case, to the template function asize, we pass a reference to an array of type T[N], whose size is N. In this case, it is array type int[8]. So the function returns N, which is size 8.

aafulei
  • 2,085
  • 12
  • 27
0

C style array's decay to pointer's when passed to a function like this. The first cout statement is printing the size of a pointer on your machine. The second cout statement is printing the size of an integer.

Use one of the following solutions in order to pass the size of the array to the function.

template<std::size_t N>
void ArraySize( int ( &array )[ N ] )
{
    std::cout << "Array size: " << N << '\n';
}

void ArraySize( int* array, std::size_t size )
{
    std::cout << "Array size: " << size << '\n';
}

template<std::size_t N>
void ArraySize( std::array<int, N>& array )
{
    std::cout << "Array size: "<< array.size( ) << '\n';
}
WBuck
  • 5,162
  • 2
  • 25
  • 36
0

sizeof(a) returns the number of bytes in array, sizeof(int) returns the number of bytes in an int, ergo sizeof(a)/sizeof(int) returns the array length

Stephen Duffy
  • 467
  • 2
  • 14
0

Easiest way to get the size of an array:

#include <iostream>

using namespace std;

int main(void) {
    int ch[5], size;

    size = sizeof(ch) / sizeof(ch[0]);

    cout << size;

    return 0;
}

Output: 5

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
0

simply divide sizeof(array1) by sizeof(int). it will give you total element in array. because sizeof(array1) will give total bytes in the array. for example sizeof(array1) = int * 8 because your array is int so int is 4 byte answer is 4*8 = 32.Now you have to divide it again by 4 cause its in byte.

cout << "Size of the Array is : " << sizeof(array1)/sizeof(int) << endl;

put above code in your main function to get result

r00tk1ll3r
  • 63
  • 1
  • 10