Skip to main content

array-total-size

array-total-size Function

Syntax:

array-total-size array → size

Arguments and Values:

array—an array.

size—a non-negative integer .

Description:

Returns the array total size of the array.

Examples:

(array-total-size (make-array 4))4 
(array-total-size (make-array 4 :fill-pointer 2))4
(array-total-size (make-array 0))0
(array-total-size (make-array(4 2)))8
(array-total-size (make-array(4 0)))0
(array-total-size (make-array()))1

Exceptional Situations:

Should signal an error of type type-error if its argument is not an array.

See Also:

make-array, array-dimensions

Notes:

If the array is a vector with a fill pointer , the fill pointer is ignored when calculating the array total size.

Since the product of no arguments is one, the array total size of a zero-dimensional array is one.

(array-total-size x)

(apply #’* (array-dimensions x))

(reduce #’* (array-dimensions x))

Expanded Reference: array-total-size

Basic Usage

array-total-size returns the total number of elements in an array, which is the product of all its dimensions.

(array-total-size (make-array 5))
=> 5

(array-total-size (make-array '(3 4)))
=> 12

(array-total-size (make-array '(2 3 4)))
=> 24

Edge Cases: Zero and Zero-Dimensional Arrays

An array with any dimension of size 0 has total size 0. A zero-dimensional array (no dimensions) has total size 1 because the product of no numbers is 1.

(array-total-size (make-array 0))
=> 0

(array-total-size (make-array '(5 0)))
=> 0

;; Zero-dimensional array: product of zero dimensions = 1
(array-total-size (make-array '()))
=> 1

Fill Pointer Is Ignored

array-total-size returns the allocated size, ignoring any fill pointer.

(let ((v (make-array 20 :fill-pointer 5)))
(list (length v) ; respects fill pointer
(array-total-size v))) ; actual allocated size
=> (5 20)

Equivalence to Product of Dimensions

array-total-size is equivalent to multiplying the values returned by array-dimensions.

(let ((a (make-array '(4 5 6))))
(= (array-total-size a)
(apply #'* (array-dimensions a))))
=> T