HOME

TheInfoList



OR:

In
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, the stride of an array (also referred to as increment, pitch or step size) is the number of locations in
memory Memory is the faculty of the mind by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action. If past events could not be remembe ...
between beginnings of successive array elements, measured in
byte The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable un ...
s 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 In physics, the principle of locality states that an object is influenced directly only by its immediate surroundings. A theory that includes the principle of locality is said to be a "local theory". This is an alternative to the concept of ins ...
, 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 struct MyRecord ; /** 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(const int *arr, int length, size_t stride) int main(void) This idiom is a form of type punning.


Array cross-section

Some languages like
PL/I PL/I (Programming Language One, pronounced and sometimes written PL/1) is a procedural, imperative computer programming language initially developed by IBM. It is designed for scientific, engineering, business and system programming. It has b ...
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 as declare some_array (12,2)fixed; an array of one dimension consisting only of the second column may be referenced as some_array(*,2)


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


References

{{DEFAULTSORT:Stride Of An Array Arrays Articles with example C code