423,113 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,113 IT Pros & Developers. It's quick & easy.

How to calculate the size of int array?

P: 23
Here's my code for the merge sort algorithm:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. void merge_(int *, int *, int *);
  4. void mergesort(int *);
  5. int main(){
  6.     int arr[]={7, 2, 1, 3, 6, 8, 10, 9, 5, 4};
  7.     mergesort(arr);
  8.     cout<<"Array after merge sorting:\n\n";
  9.     for(int i=0; i<10; i++){
  10.         cout<<arr[i]<<"\t";
  11.     }
  12.     return 0;
  13. }
  14. void merge_(int *l, int *r, int *arr){
  15.     int i=0, j=0, k=0;
  16.     int nL, nR;
  17.     nL=sizeof(l)/sizeof(l[0]);
  18.     nL=sizeof(r)/sizeof(r[0]);
  19.     while(i<nL && j<nR){
  20.         if(l[i]<=r[j]){
  21.             arr[k]=l[i];
  22.             k++;
  23.             i++;
  24.         }
  25.         else{
  26.             arr[k]=r[j];
  27.             k++;
  28.             j++;
  29.         }
  30.     }
  31.     while(i<nL){
  32.         arr[k]=l[i];
  33.         i++;
  34.         k++;
  35.     }
  36.     while(j<nL){
  37.         arr[k]=r[i];
  38.         j++;
  39.         k++;
  40.     }
  41. }
  42. void mergesort(int *arr){
  43.     int n;
  44.     n=sizeof(arr)/sizeof(arr[0]);
  45.     //cout<<n<<"\n";
  46.     if(n<2){
  47.         return;
  48.     }
  49.     int mid;
  50.     mid=n/2;
  51.     int left[mid];
  52.     int right[n-mid];
  53.     int i;
  54.     for(i=0; i<(mid-1); i++){
  55.         left[i]=arr[i];
  56.     }
  57.     for(i=mid; i<n; i++){
  58.         right[i-mid]=arr[i];
  59.     }
  60.     mergesort(left);
  61.     mergesort(right);
  62.     merge_(left, right, arr);
  63. }
  64.  
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?
Jun 9 '18 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,150
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:

https://bytes.com/topic/c/insights/7...rrays-revealed
Jun 9 '18 #2

Expert 100+
P: 2,373
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.
Jun 11 '18 #3

palak10
P: 3
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 :)
Jun 12 '18 #4

Expert 100+
P: 2,373
@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.

@weaknessforcats 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.
Jun 12 '18 #5

Post your reply

Sign in to post your reply or Sign up for a free account.