Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Stride of an array
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{refimprove|date=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 [[computer memory|memory]] between beginnings of successive [[Array data structure|array]] elements, measured in [[byte]]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 array|2D]] or [[multi-dimensional array]]s, depending on the effects of [[CPU cache|caching]] and the [[memory access pattern|access pattern]]s used.{{citation needed|date=January 2018}} This can be attributed to the [[Locality of reference|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 [[AoS_and_SoA#Array of structures|arrays of structures]] to be treated as overlapping [[parallel array]]s with non-unit stride: <syntaxhighlight lang="c"> #include <stdio.h> struct MyRecord { int value; 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. */ 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; } </syntaxhighlight> 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.<ref>{{cite book|last=Hughes|first=Joan K|title=PL/I Structured Programming (second ed.)|year=1979|publisher=John Wiley and Sons|location=New York|isbn=0-471-01908-9|url-access=registration|url=https://archive.org/details/plistructuredpr00hugh}}</ref>{{rp|p.262}} For example, if a two-dimensional array is declared as <syntaxhighlight lang="m2"> declare some_array (12,2)fixed; </syntaxhighlight> an array of one dimension consisting only of the second column may be referenced as <syntaxhighlight lang="m2"> some_array(*,2) </syntaxhighlight> ===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: <syntaxhighlight lang="java"> public class GrayscaleImage { private final int width, height, widthStride; /** Pixel data. Pixel in single row are always considered contiguous in this example. */ private final byte[] pixels; /** Offset of the first pixel within pixels */ private final int offset; /** Constructor for contiguous data */ public Image(int width, int height, byte[] pixels) { this.width = width; this.height = height; this.pixels = pixels; this.offset = 0; this.widthStride = width; } /** Subsection constructor */ public Image(int width, int height, byte[] pixels, int offset, int widthStride) { 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. */ public Image crop(int x1, int y1, int x2, int y2) { return new Image(x2 - x1, y2 - y1, pixels, offset + y1 * widthStride + x1, widthStride); } /** Returns pixel value at specified coordinate */ public byte getPixelAt(int x, int y) { return pixels[offset + y * widthStride + x]; } } </syntaxhighlight> ==References== {{Reflist}} {{DEFAULTSORT:Stride Of An Array}} [[Category:Arrays]] [[Category:Articles with example C code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Citation needed
(
edit
)
Template:Cite book
(
edit
)
Template:Refimprove
(
edit
)
Template:Reflist
(
edit
)
Template:Rp
(
edit
)