Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Function that returns the size of an int array
    I create a simple function that returns the size of an int array, but it always returns 1 (sizeof(a) = 4 and sizeof(int) = 4) But when I put everything in the main function, it displays the correct answer.
    #include <iostream>
    using namespace std;
    
    int getsize(int a[]){
    	return sizeof(a) / sizeof(a[0]);
    };
    
    int main(){
    	int a[] = {1,2,3,4,5,6,7,8};
    	cout << getsize(a) << endl;
    	return 0;
    }
    
    So, please help me to correct the function getsize() to return a proper value. Thank you =)
    hkhoi posted this question on 1/26/14 | array, cpp
    Answers
  • +
  • 2
  • -
  • The array you are using here is a static-sized low-level C array, since it has both of these properties the programmer is the one tasked with keeping count of the objects inside the array, the only reason to sizeof such an array would be if the code itself is dynamically generated. The easiest way out of this is to use either a standard C++ vector (or list or stack or whatever):
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main(){
      vector<int> a(8);
      for(int i=0;i<8;i++) a[i]=i+1;
      cout << a.size() << endl;
      return 0;
    }
    
    or if you insist on a static size the new array class template from C++11 can work for you:
    #include <iostream>
    #include <array>
    using namespace std;
    
    int main(){
      array<int,8> a;
      for(int i=0;i<8;i++) a[i]=i+1;
      cout << a.size() << endl;
      return 0;
    }
    
    Going back to doing things the C way, your best bet is to simply store the size in a pre-processor directive:
    #include <iostream>
    using namespace std;
    
    int main(){
    #define a_SIZE 8
      int a[a_SIZE] = {1,2,3,4,5,6,7,8};
      cout << a_SIZE << endl;
      return 0;
    }
    
    or if you really need to guess the size:
    #include <iostream>
    using namespace std;
    
    #define get_array_size(arr) (sizeof(arr) / sizeof(arr[0]))
    
    int main(){
      int a[] = {1,2,3,4,5,6,7,8};
    #define a_SIZE get_array_size(a)
      cout << a_SIZE << endl;
      return 0;
    } 
    
    The compiler should store the size as a constant in the last two (possibly three) cases, since sizeof is calculated at compile time.
  • +
  • 2
  • -
  • In main, the name array is an array so you get the size in bytes of the array with sizeof. However, an array decays to a pointer when passed to a function, so you get sizeof(int*) inside the function. Be aware that taking an argument in the form of T arg[] is exactly the same as taking the argument as T* arg. So your function is the exact equivalent of
    void function(int* arg) {
        cout << sizeof(arg) << endl;
    }
    
    Correction to your code is as below:
    #include <iostream>
    using namespace std;
    
    int getsize(int (&a)[8]){
    	return sizeof(a) / sizeof(a[0]);
    };
    
    int main(){
    	int a[] = {1,2,3,4,5,6,7,8};
    	cout << getsize(a) << endl;
    	return 0;
    }
    
    Log in to write an answer.