Stride of an array
In computer programming, the stride of an array is the number of locations in memory between beginnings of successive array elements, measured in bytes or in units of the size of the array's elements. The stride cannot be smaller than the element size but can be larger, indicating extra space between elements.
An array with stride of exactly the same size as the size of each of its elements is contiguous in memory. Such arrays are sometimes said to have unit stride. Unit stride arrays are sometimes more efficient than non-unit stride arrays, but non-unit stride arrays can be more efficient for 2D or multi-dimensional arrays, depending on the effects of caching and the access patterns used. This can be attributed to the principle of locality, specifically spatial locality.
Reasons for non-unit stride
Arrays may have a stride larger than their elements' width in bytes in at least two cases:Overlapping parallel arrays
Some languages allow arrays of structures to be treated as overlapping parallel arrays with non-unit stride:- include
/** Print the contents of an array of ints with the given stride.
Note that size_t is the correct type, as int can overflow. */
void print_some_ints
int main
This idiom is a form of type punning.
Array cross-section
Some languages like PL/I or Fortran allow what is known as an array cross-section, which selects certain columns or rows from a larger array. For example, if a two-dimensional array is declared asdeclare some_array fixed;
an array of one dimension consisting only of the second column may be referenced as
some_array
Example of multidimensional array with non-unit stride
Non-unit stride is particularly useful for images. It allows for creating subimages without copying the pixel data. Java example:public class GrayscaleImage