How to calculate the size of int array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    How to calculate the size of int array?

    Here's my code for the merge sort algorithm:

    Code:
    #include <iostream>
    using namespace std;
    void merge_(int *, int *, int *);
    void mergesort(int *);
    int main(){
        int arr[]={7, 2, 1, 3, 6, 8, 10, 9, 5, 4};
        mergesort(arr);
        cout<<"Array after merge sorting:\n\n";
        for(int i=0; i<10; i++){
            cout<<arr[i]<<"\t";
        }
        return 0;
    }
    void merge_(int *l, int *r, int *arr){
        int i=0, j=0, k=0;
        int nL, nR;
        nL=sizeof(l)/sizeof(l[0]);
        nL=sizeof(r)/sizeof(r[0]);
        while(i<nL && j<nR){
            if(l[i]<=r[j]){
                arr[k]=l[i];
                k++;
                i++;
            }
            else{
                arr[k]=r[j];
                k++;
                j++;
            }
        }
        while(i<nL){
            arr[k]=l[i];
            i++;
            k++;
        }
        while(j<nL){
            arr[k]=r[i];
            j++;
            k++;
        }
    }
    void mergesort(int *arr){
        int n;
        n=sizeof(arr)/sizeof(arr[0]);
        //cout<<n<<"\n";
        if(n<2){
            return;
        }
        int mid;
        mid=n/2;
        int left[mid];
        int right[n-mid];
        int i;
        for(i=0; i<(mid-1); i++){
            left[i]=arr[i];
        }
        for(i=mid; i<n; i++){
            right[i-mid]=arr[i];
        }
        mergesort(left);
        mergesort(right);
        merge_(left, right, arr);
    }
    In the meregesort function, I want to store the size of the array 'arr' in the variable 'n'. As strlen() doesn't work with integer arrays, I am using this expression to calculate the size: n=sizeof(arr)/sizeof(arr[0]).

    But this expression is storing '1' in n (maybe treating arr just as an int type pointer, therefore 4/4=1) and hence since 1<2 , the function is just simply returning.

    How to calculate the size of the passed array?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You cannot calculate the size of an array from the name of the array variable. By definition the name of the array variable is a pointer to element 0. From that pointer you cannot calculate the size of the array as there is no array, just a pointer.

    Read this:

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      That's why standard library functions such as qsort() and bsearch() have two arguments for array inputs: pointer to start of the array and also the number of elements in the array.

      Comment

      • palak10
        New Member
        • Jun 2018
        • 3

        #4
        In this case you can sizeof. sizeof returns the amount of Bytes the element is using. So the idea is to find the sizeof(array) (in your case which will be 20) and the sizeof(int). The size of an int is 4 Bytes.

        So now you can use the Math and figure out how to find the array size using this two values.

        Hope that helps :)

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          @palak10, that will not work in this case. Look at lines 17-18 of the original post to see that @dev7060 tried exactly what you propose. The point of the original post was to find out why it didn't work and to ask what should be done instead.

          @weaknessforcat s provided a succinct explanation why it didn't work and referred the OP to the detailed explanation in Arrays Revealed.

          I used Standard Library examples to suggest how it could be done.

          Comment

          Working...