 in computer programming, the stride of an array (also referred to as increment, pitch or step size) 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 three cases: === padding=== many languages (including c and c++) allow structures to be padded to better take advantage either of the word length and/or cache line size of the machine. for example: struct a { int a; char b; }; struct a myarray[100]; in the above code snippet, myarray might well turn out to have a stride of eight bytes, rather than five, if the c code were compiled for a 32-bit 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 arrays with non-unit stride: #include struct myrecord { int value; char *text; }; /* print the contents of an array of ints with the given stride */ void print_some_ints(const int *arr, int length, size_t stride) { int i; printf("address\t\tvalue\n"); for (i=0; i < length; ++i) { printf("%p\t%d\n", arr, arr[0]); arr = (int *)((unsigned char *)arr + stride); } } int main(void) { int ints[100] = {0}; struct myrecord records[100] = {0}; print_some_ints(&ints[0], 100, sizeof ints[0]); print_some_ints(&records[0].value, 100, sizeof records[0]); return 0; } this idiom is a form of type punning. ===array cross-section === some languages like pl/i allow what is known as an array cross-section, which select 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 consisting only of the second column may be referenced as some_array(*,2) ===example of mult
Arrays