473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to calculate the size of int array?

dev7060
636 Expert 512MB
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
4 2088
weaknessforcats
9,208 Expert Mod 8TB
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
donbock
2,426 Expert 2GB
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
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
donbock
2,426 Expert 2GB
@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

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

Similar topics

5
by: Sunil Menon | last post by:
Dear All, Given a Hash Table containing "n" objects (each having many properties) - is it possible to know how much memory that hash table has taken - in a simple console application? Please...
0
by: Chris | last post by:
I have a fairly straight-forward form that loads preferences upon running, and saves them upon exiting. Included in the preferences are things like window size & location, working directory, etc. ...
2
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
4
by: pmatos | last post by:
Hi all, How can I compute the size of vector<int> given iterators to it. Can I do : int size = itEnd - itBegin; ??? Cheers, Paulo Matos
4
by: Angus Comber | last post by:
Hello If I do this: struct mystruct { long nKey; char szItem; };
13
by: Manish_Ganvir | last post by:
Please do not use pointer arithmetic or for loops Solution
4
by: Yaro | last post by:
Hi, I am looking for information how calculate size of table with BLOB field. After RUNSTATS command select fpages * tablespace pagesize - gives me only 16kB (table contain about 10M) I...
1
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define...
1
by: Charming12 | last post by:
Hi All, This might be looking a naive question, but i am facing a big issue because of this. I have a class written in C# .Net which contains one more class. And i need to pass the size of this...
1
by: jairam84 | last post by:
Hi, I need to find out the the total size of a partition and how much of it has been utilized. How to calculate the percentage of utilization of a partition in DB2 of Z/OS. Thanks
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.