Stride of an array

Last updated

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.

Contents

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.

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:

structA{inta;charb;};structAmyArray[100];

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 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<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.

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. [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)

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:

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];}}

Related Research Articles

In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. It is a kind of lazy evaluation that refers specifically to the instantiation of objects or other resources.

The BMP file format or bitmap, is a raster graphics image file format used to store bitmap digital images, independently of the display device, especially on Microsoft Windows and OS/2 operating systems.

<span class="mw-page-title-main">C syntax</span> Set of rules defining correctly structured programs

The syntax of the C programming language is the set of rules governing writing of software in the C language. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

<span class="mw-page-title-main">Pointer (computer programming)</span> Object which stores memory addresses in a computer program

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.

<span class="mw-page-title-main">Foreach loop</span> Control flow statement for traversing items in a collection

In computer programming, foreach loop is a control flow statement for traversing items in a collection. foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal.

In computing, aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program. Thus, modifying the data through one name implicitly modifies the values associated with all aliased names, which may not be expected by the programmer. As a result, aliasing makes it particularly difficult to understand, analyze and optimize programs. Aliasing analysers intend to make and compute useful information for understanding aliasing in programs.

A struct in the C programming language is a composite data type declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the same address. The struct data type can contain other data types so is used for mixed-data-type records such as a hard-drive directory entry, or other mixed-type records.

In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such, it is a programming error and a common source of bugs in software.

The computer programming languages C and Pascal have similar times of origin, influences, and purposes. Both were used to design their own compilers early in their lifetimes. The original Pascal definition appeared in 1969 and a first compiler in 1970. The first version of C appeared in 1972.

<span class="mw-page-title-main">C data types</span> Data types supported by the C programming language

In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determine the types of operations or methods of processing of data elements.

Data structure alignment is the way data is arranged and accessed in computer memory. It consists of three separate but related issues: data alignment, data structure padding, and packing.

sizeof is a unary operator in the programming languages C and C++. It generates the storage size of an expression or a data type, measured in the number of char-sized units. Consequently, the construct sizeof (char) is guaranteed to be 1. The actual number of bits of type char is specified by the preprocessor macro CHAR_BIT, defined in the standard include file limits.h. On most modern computing platforms this is eight bits. The result of sizeof has an unsigned integer type that is usually denoted by size_t.

XImage is the X client side storage mechanism for an X Window System pixel map. The structure of an XImage as defined by the X Window core protocol is the following:

The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages are often integrated into a single product, with the programmer able to specify C or C++ as their source language.

Silicon Graphics Image (SGI) or the RGB file format is the native raster graphics file format for Silicon Graphics workstations. The format was invented by Paul Haeberli. It can be run-length encoded (RLE). FFmpeg and ImageMagick, among others, support this format.

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 language.

select is a system call and application programming interface (API) in Unix-like and POSIX-compliant operating systems for examining the status of file descriptors of open input/output channels. The select system call is similar to the poll facility introduced in UNIX System V and later operating systems. However, with the c10k problem, both select and poll have been superseded by the likes of kqueue, epoll, /dev/poll and I/O completion ports.

ALGOL 68RS is the second ALGOL 68 compiler written by I. F. Currie and J. D. Morrison, at the Royal Signals and Radar Establishment (RSRE). Unlike the earlier ALGOL 68-R, it was designed to be portable, and implemented the language of the Revised Report.

In the C programming language, operations can be performed on a bit level using bitwise operators.

C struct data types may end with a flexible array member with no specified size:

References

  1. Hughes, Joan K (1979). PL/I Structured Programming (second ed.) . New York: John Wiley and Sons. ISBN   0-471-01908-9.