Difference between revisions of "Compute the number of elements in an array"
From CodeCodex
(→C/C++) |
|||
| (5 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
| + | Alternative titles for this article: | ||
| + | * [[Calculate the size of an array]]. | ||
| + | * [[Count the number of array elements]]. | ||
| + | * [[Array size]]. | ||
| + | |||
==Implementations== | ==Implementations== | ||
===C/C++=== | ===C/C++=== | ||
There is a convenient compile-time trick to get the length of a statically-allocated array, within the scope of where it is declared. (This works because the compiler can just look up to where it is declared, and read the constant length, and hard-code the number.) The trick to computing the number of elements in an array is to divide the total size of an array by the size of a given element. For example, if the total size of the array is 80 bytes and each element takes up 8 bytes, there must be 10 elements in the array. | There is a convenient compile-time trick to get the length of a statically-allocated array, within the scope of where it is declared. (This works because the compiler can just look up to where it is declared, and read the constant length, and hard-code the number.) The trick to computing the number of elements in an array is to divide the total size of an array by the size of a given element. For example, if the total size of the array is 80 bytes and each element takes up 8 bytes, there must be 10 elements in the array. | ||
| − | < | + | <pre class="c"> |
sizeof(array) / sizeof(array[0]); | sizeof(array) / sizeof(array[0]); | ||
| − | </ | + | </pre> |
Ideally, you would put this into a macro as follows: | Ideally, you would put this into a macro as follows: | ||
| − | < | + | <pre class="c"> |
#define NumElm(array) (sizeof(array) / sizeof((array)[0])) | #define NumElm(array) (sizeof(array) / sizeof((array)[0])) | ||
| − | </ | + | </pre> |
<b>Note</b>: this method will not work for arrays that are zero- or null-terminated (including strings). | <b>Note</b>: this method will not work for arrays that are zero- or null-terminated (including strings). | ||
| Line 14: | Line 19: | ||
In C99, there are variable-length arrays (VLAs). The above trick should also work to find the length of VLAs. However, passing VLAs to functions also requires their lengths to be passed in separately or be known. | In C99, there are variable-length arrays (VLAs). The above trick should also work to find the length of VLAs. However, passing VLAs to functions also requires their lengths to be passed in separately or be known. | ||
| + | |||
| + | There is _countof macro in Visual Studio | ||
===C++=== | ===C++=== | ||
In C++, people often use STL data structures, like std::vector, instead of arrays, because of their better object-oriented interface. | In C++, people often use STL data structures, like std::vector, instead of arrays, because of their better object-oriented interface. | ||
| − | < | + | <pre class="cpp"> |
std::vector myVector; | std::vector myVector; | ||
myVector.size(); | myVector.size(); | ||
| − | </ | + | </pre> |
===Go=== | ===Go=== | ||
| − | < | + | <pre> |
// getting the length of an array is silly, because the length is part of the array's static type | // getting the length of an array is silly, because the length is part of the array's static type | ||
myArray := [3]int{1, 2, 3} | myArray := [3]int{1, 2, 3} | ||
| Line 30: | Line 37: | ||
mySlice := []int{1, 2, 3} | mySlice := []int{1, 2, 3} | ||
fmt.Println(len(mySlice)) // prints 3 | fmt.Println(len(mySlice)) // prints 3 | ||
| − | </ | + | </pre> |
===Haskell=== | ===Haskell=== | ||
Haskell often uses lists instead of arrays: | Haskell often uses lists instead of arrays: | ||
| − | < | + | <pre> |
length myList | length myList | ||
| − | </ | + | </pre> |
If you must use arrays, Haskell's arrays can be bounded by any type that implements Ix, and are not necessarily integers. You can try to figure out the length from the bounds. | If you must use arrays, Haskell's arrays can be bounded by any type that implements Ix, and are not necessarily integers. You can try to figure out the length from the bounds. | ||
| − | < | + | <pre> |
low, high = bounds myArray | low, high = bounds myArray | ||
| − | </ | + | </pre> |
===Java=== | ===Java=== | ||
| − | < | + | <pre class="java"> |
int[] myArray; | int[] myArray; | ||
myArray.length; | myArray.length; | ||
| − | </ | + | </pre> |
for Collection's: | for Collection's: | ||
| − | < | + | <pre class="java"> |
Collection myCollection; | Collection myCollection; | ||
myCollection.size(); | myCollection.size(); | ||
| − | </ | + | </pre> |
===JavaScript=== | ===JavaScript=== | ||
| − | < | + | <pre class="javascript"> |
var myArray = new Array(5); | var myArray = new Array(5); | ||
myArray.length; | myArray.length; | ||
| − | </ | + | </pre> |
===MivaScript=== | ===MivaScript=== | ||
| − | < | + | <pre class="php"> |
<mvassign name="l.arrays" index="1" value="{'one array'}"> | <mvassign name="l.arrays" index="1" value="{'one array'}"> | ||
<mvassign name="l.arrays" index="2" value="{'two array'}"> | <mvassign name="l.arrays" index="2" value="{'two array'}"> | ||
<mvassign name="l.length" value="{miva_array_max(l.arrays)}"> | <mvassign name="l.length" value="{miva_array_max(l.arrays)}"> | ||
| − | </ | + | </pre> |
===OCaml=== | ===OCaml=== | ||
| − | < | + | <pre> |
Array.length myArray;; | Array.length myArray;; | ||
List.length myList;; | List.length myList;; | ||
| − | </ | + | </pre> |
===Perl=== | ===Perl=== | ||
| − | < | + | <pre> |
@myArray = (); | @myArray = (); | ||
scalar(@myArray); | scalar(@myArray); | ||
| − | </ | + | </pre> |
===PHP=== | ===PHP=== | ||
| − | < | + | <pre class="php"> |
$myArray = array(); | $myArray = array(); | ||
count($myArray); | count($myArray); | ||
| − | </ | + | </pre> |
===Python=== | ===Python=== | ||
| − | < | + | <pre class="python"> |
mySeq = [] | mySeq = [] | ||
len(mySeq) | len(mySeq) | ||
| − | </ | + | </pre> |
=== Ruby === | === Ruby === | ||
| − | < | + | <pre class="ruby"> |
lst = [1,2,3,4,5] | lst = [1,2,3,4,5] | ||
lst.size #returns 5 | lst.size #returns 5 | ||
| − | </ | + | </pre> |
===Scheme=== | ===Scheme=== | ||
| − | < | + | <pre> |
(length my-list) | (length my-list) | ||
(vector-length my-vector) | (vector-length my-vector) | ||
| − | </ | + | </pre> |
| + | ===Seed7=== | ||
| + | <pre> | ||
| + | length(myArray) | ||
| + | </pre> | ||
[[Category:Arrays]] | [[Category:Arrays]] | ||
| + | |||
[[Category:C]] | [[Category:C]] | ||
[[Category:C++]] | [[Category:C++]] | ||
| Line 111: | Line 123: | ||
[[Category:Perl]] | [[Category:Perl]] | ||
[[Category:Python]] | [[Category:Python]] | ||
| − | |||
[[Category:Ruby]] | [[Category:Ruby]] | ||
| + | [[Category:Scheme]] | ||
| + | [[Category:Seed7]] | ||
Latest revision as of 21:14, 20 March 2011
Alternative titles for this article:
Contents |
[edit] Implementations
[edit] C/C++
There is a convenient compile-time trick to get the length of a statically-allocated array, within the scope of where it is declared. (This works because the compiler can just look up to where it is declared, and read the constant length, and hard-code the number.) The trick to computing the number of elements in an array is to divide the total size of an array by the size of a given element. For example, if the total size of the array is 80 bytes and each element takes up 8 bytes, there must be 10 elements in the array.
sizeof(array) / sizeof(array[0]);
Ideally, you would put this into a macro as follows:
#define NumElm(array) (sizeof(array) / sizeof((array)[0]))
Note: this method will not work for arrays that are zero- or null-terminated (including strings).
Note: This method will not be able to "find" the length of an array given just the pointer to its first element, because the "length" is not stored anywhere at runtime. Arrays cannot be passed or returned from functions; only the pointer to its first element can. So for example, a function which takes in an array and which needs to know its length will need to take in both the pointer to the first argument and its length as separate arguments. Similarly, dynamically-allocated arrays, which exist only as pointers at compile-time, will need to have their lengths kept track of separately, if you need to know its length.
In C99, there are variable-length arrays (VLAs). The above trick should also work to find the length of VLAs. However, passing VLAs to functions also requires their lengths to be passed in separately or be known.
There is _countof macro in Visual Studio
[edit] C++
In C++, people often use STL data structures, like std::vector, instead of arrays, because of their better object-oriented interface.
std::vector myVector; myVector.size();
[edit] Go
// getting the length of an array is silly, because the length is part of the array's static type
myArray := [3]int{1, 2, 3}
fmt.Println(len(myArray)) // prints 3
// getting the length of a slice
mySlice := []int{1, 2, 3}
fmt.Println(len(mySlice)) // prints 3
[edit] Haskell
Haskell often uses lists instead of arrays:
length myList
If you must use arrays, Haskell's arrays can be bounded by any type that implements Ix, and are not necessarily integers. You can try to figure out the length from the bounds.
low, high = bounds myArray
[edit] Java
int[] myArray; myArray.length;
for Collection's:
Collection myCollection; myCollection.size();
[edit] JavaScript
var myArray = new Array(5); myArray.length;
[edit] MivaScript
<mvassign name="l.arrays" index="1" value="{'one array'}">
<mvassign name="l.arrays" index="2" value="{'two array'}">
<mvassign name="l.length" value="{miva_array_max(l.arrays)}">
[edit] OCaml
Array.length myArray;; List.length myList;;
[edit] Perl
@myArray = (); scalar(@myArray);
[edit] PHP
$myArray = array(); count($myArray);
[edit] Python
mySeq = [] len(mySeq)
[edit] Ruby
lst = [1,2,3,4,5] lst.size #returns 5
[edit] Scheme
(length my-list) (vector-length my-vector)
[edit] Seed7
length(myArray)