This article needs additional citations for verification .(December 2009) |
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.[ citation needed ] This can be attributed to the principle of locality, specifically spatial locality.
Arrays may have a stride larger than their elements' width in bytes in at least two cases:
Some languages allow arrays of structures to be treated as overlapping parallel arrays with non-unit stride:
#include<stdio.h>structMyRecord{intvalue;char*text;};/** Print the contents of an array of ints with the given stride. Note that size_t is the correct type, as int can overflow. */voidprint_some_ints(constint*arr,intlength,size_tstride){inti;printf("Address\t\tValue\n");for(i=0;i<length;++i){printf("%p\t%d\n",arr,arr[0]);arr=(int*)((unsignedchar*)arr+stride);}}intmain(void){intints[100]={0};structMyRecordrecords[100]={0};print_some_ints(&ints[0],100,sizeofints[0]);print_some_ints(&records[0].value,100,sizeofrecords[0]);return0;}
This idiom is a form of type punning.
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. [1] : p.262 For example, if a two-dimensional array is declared as
declaresome_array(12,2)fixed;
an array of one dimension consisting only of the second column may be referenced as
some_array(*,2)
Non-unit stride is particularly useful for images. It allows for creating subimages without copying the pixel data. Java example:
publicclassGrayscaleImage{privatefinalintwidth,height,widthStride;/** Pixel data. Pixel in single row are always considered contiguous in this example. */privatefinalbyte[]pixels;/** Offset of the first pixel within pixels */privatefinalintoffset;/** Constructor for contiguous data */publicImage(intwidth,intheight,byte[]pixels){this.width=width;this.height=height;this.pixels=pixels;this.offset=0;this.widthStride=width;}/** Subsection constructor */publicImage(intwidth,intheight,byte[]pixels,intoffset,intwidthStride){this.width=width;this.height=height;this.pixels=pixels;this.offset=offset;this.widthStride=widthStride;}/** Returns a subregion of this Image as a new Image. This and the new image share the pixels, so changes to the returned image will be reflected in this image. */publicImagecrop(intx1,inty1,intx2,inty2){returnnewImage(x2-x1,y2-y1,pixels,offset+y1*widthStride+x1,widthStride);}/** Returns pixel value at specified coordinate */publicbytegetPixelAt(intx,inty){returnpixels[offset+y*widthStride+x];}}