Stride of an array

Stride of an array

In computer programming, the stride of an array (also increment or step size) refers to the number of locations in memory between successive array elements, measured in bytes or in units of the size of the array's elements.

An array with stride 1 has elements which are contiguous in memory. Such arrays are sometimes said to have unit stride. Unit stride arrays are generally more efficient than non-unit stride arrays, due to the effects of caching. This is attributed to the Principle of Locality, specifically spatial locality.

Reasons for non-unit stride

There are at least two reasons arrays may have a stride larger than their elements' width in bytes. First, many languages (including C and C++) allow structures to be padded to better take advantage of the word length of the machine. For example:

struct ThreeBytesWide {
    char a[3];
};
 
struct ThreeBytesWide myArray[100];

In the above code snippet, myArray might well turn out to have a stride of four bytes, rather than three, 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.

Second, some languages allow arrays of structures to be treated as overlapping parallel arrays with non-unit stride:

#include <stdio.h>
 
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;
    for (i=0; i < length; ++i) {
        printf("%d\n", 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.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Stride — can stand for: * STRIDE (MALAYSIA),Science And Technology Research Institute For Defence * A step (ie. part of walking) * In music: ** STRIDE An indie rock n roll band from North East Scotland ** Stride (music), a type of piano playing ** Stride… …   Wikipedia

  • Array slicing — In computer programming, array slicing is an operation that extracts certain elements from an array and packages them as another array, possibly with different number of indices (or dimensions) and different index ranges. Two common examples are… …   Wikipedia

  • stride — 1. noun /straɪd/ a) A long step. This stride value is generally equal to the pixel width of the bitmap times the number of bytes per pixel, but for performance reasons it might be rounded... b) The number of memory locations between successive… …   Wiktionary

  • Data structure alignment — is the way data is arranged and accessed in computer memory. It consists of two separate but related issues: data alignment and data structure padding. When a modern computer reads from or writes to a memory address, it will do this in word sized …   Wikipedia

  • Инкремент — Инкремент, инкрементирование (от англ. increment «увеличение»)  операция во многих языках программирования, увеличивающая переменную. Обратную операцию называют декремент (уменьшение). Чаще всего унарная операция приводит переменную к… …   Википедия

  • Dope vector — In computer programming, a dope vector is a data structure used to hold information about a data object,[1] e.g. an array, especially its memory layout. A dope vector typically contains information about the type of array element, rank of an… …   Wikipedia

  • Type punning — FORCETOC In computer science, type punning is a common term for 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 …   Wikipedia

  • Business and Industry Review — ▪ 1999 Introduction Overview        Annual Average Rates of Growth of Manufacturing Output, 1980 97, Table Pattern of Output, 1994 97, Table Index Numbers of Production, Employment, and Productivity in Manufacturing Industries, Table (For Annual… …   Universalium

  • performing arts — arts or skills that require public performance, as acting, singing, or dancing. [1945 50] * * * ▪ 2009 Introduction Music Classical.       The last vestiges of the Cold War seemed to thaw for a moment on Feb. 26, 2008, when the unfamiliar strains …   Universalium

  • Life Sciences — ▪ 2009 Introduction Zoology       In 2008 several zoological studies provided new insights into how species life history traits (such as the timing of reproduction or the length of life of adult individuals) are derived in part as responses to… …   Universalium

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”