How to find integer array size in java
The length of an array is available as
int l = array.length;
The size of a List
is availabe as
int s = list.size();
How do I determine the size of my array in C?
Executive summary:
int a[17];
size_t n = sizeof(a)/sizeof(a[0]);
Full answer:
To determine the size of your array in bytes, you can use the sizeof
operator:
int a[17];
size_t n = sizeof(a);
On my computer, ints are 4 bytes long, so n is 68.
To determine the number of elements in the array, we can divide
the total size of the array by the size of the array element.
You could do this with the type, like this:
int a[17];
size_t n = sizeof(a) / sizeof(int);
and get the proper answer (68 / 4 = 17), but if the type ofa
changed you would have a nasty bug if you forgot to change
the sizeof(int)
as well.
So the preferred divisor is sizeof(a[0])
or the equivalent sizeof(*a)
, the size of the first element of the array.
int a[17];
size_t n = sizeof(a) / sizeof(a[0]);
Another advantage is that you can now easily parameterize
the array name in a macro and get:
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
int a[17];
size_t n = NELEMS(a);
Finding the size of int [] array
Remember, in C when you pass an array as an argument to a function, you're passing a pointer to the array. If you want to pass the size of the array, you should pass it as a separated argument.
The size of a pointer and an int
is 4 or 8 or something else - depending on ABI
.
In your case, it's 4
, so you're getting sizeof(int *)/sizeof int
which is 1.
Here is a useful trick
You can store the length of the array in the first element of it:
int myArray[]= {-1, 1, 2, 3, 4, 5};
myArray[0] = sizeof(myArray) / sizeof(myArray[0]) - 1;
//The -1 because.. the first element is only to indicate the size
Now, myArray[0]
will contain the size of the array.
How to find the size of integer array
If the array is a global, static, or automatic variable (int array[10];
), then sizeof(array)/sizeof(array[0])
works.
If it is a dynamically allocated array (int* array = malloc(sizeof(int)*10);
) or passed as a function argument (void f(int array[])
), then you cannot find its size at run-time. You will have to store the size somewhere.
Note that sizeof(array)/sizeof(array[0])
compiles just fine even for the second case, but it will silently produce the wrong result.
C++ - how to find the length of an integer
The number of digits of an integer n
in any base is trivially obtained by dividing until you're done:
unsigned int number_of_digits = 0;
do {
++number_of_digits;
n /= base;
} while (n);
How do I get the size of an int*?
You could get the size of of a pointer to an integer with sizeof(int*)
.
However, given a pointer value, there is no portable way (in C or C++) to get the dynamic -runtime- size of the pointed memory zone (in heap or elsewhere).
A general advice is to avoid raw pointers when possible: use smart pointers (from <memory>
header) and standard C++ containers (e.g. std::vector
from <vector>
)
So if you declared std::vector<int> values;
you could get the vector size with values.size();
Find the Size of integer array received as an argument to a function in c
You cannot do it that way. When you pass an array to a function, it decays into a pointer to the first element, at which point knowledge of its size is lost.
If you want to know the size of an array passed to the function, you need to work it out before decay and pass that information with the array, something like:
void function (size_t sz, int *arr) { ... }
:
{
int x[20];
function (sizeof(x)/sizeof(*x), x);
}
Related Topics
Why Are Memcpy() and Memmove() Faster Than Pointer Increments
How to Read File Content into Istringstream
Move-Only Version of Std::Function
Why Is It Illegal to Take the Address of an Rvalue Temporary
How to Properly Free the Memory Allocated by Placement New
How to Make Generic Computations Over Heterogeneous Argument Packs of a Variadic Template Function
How to Parse Date/Time from String
Difference Between [Square Brackets] and *Asterisk
Is There a Function to Copy an Array in C/C++
Iterating C++ Vector from the End to the Beginning
How to Correctly and Standardly Compare Floats
Value Initialization and Non Pod Types
Addition of Two Chars Produces Int
Os Specific Instructions in Cmake: How To
Is There a Range Class in C++11 for Use with Range Based for Loops