HOME

TheInfoList



OR:

In computer programming, 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 remembered, ...
between beginnings of successive
array An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
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 uni ...
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 array In computer science, array is a data type that represents a collection of ''elements'' (values or variables), each selected by one or more indices (identifying keys) that can be computed at run time during program execution. Such a collection ...
s, 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 three cases:


Padding

Many languages (including C and
C++ C, or c, is the third letter in the Latin alphabet, used in the modern English alphabet, the alphabets of other western European languages and others worldwide. Its name in English is ''cee'' (pronounced ), plural ''cees''. History "C" ...
) allow structures to be padded to better take advantage either of the
word length In computing, a word is the natural unit of data used by a particular processor design. A word is a fixed-sized datum handled as a unit by the instruction set or the hardware of the processor. The number of bits or digits in a word (the ''word ...
and/or cache line size of the machine. For example: struct A ; struct A myArray 00 In the above code snippet, myArray might well turn out to have a stride of eight bytes, rather than five (4 bytes for the int plus one for the char), if the C code were compiled for a
32-bit In computer architecture, 32-bit computing refers to computer systems with a processor, memory, and other major system components that operate on data in 32-bit units. Compared to smaller bit widths, 32-bit computers can perform large calculati ...
architecture, and the compiler had optimized (as is usually the case) for minimum processing time rather than minimum memory usage.


Overlapping parallel arrays

Some languages allow arrays of structures to be treated as overlapping
parallel array In computing, a group of parallel arrays (also known as structure of arrays or SoA) is a form of implicit data structure that uses multiple arrays to represent a singular array of records. It keeps a separate, homogeneous data array for each field ...
s 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 In computer science, a type punning is any programming technique that subverts or circumvents the type system of a programming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal ...
.


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 developed and published by IBM. It is designed for scientific, engineering, business and system programming. I ...
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