What is a strided array? What is a strided array? arrays arrays

What is a strided array?


Say you have a structure

struct SomeStruct {    int someField;    int someUselessField;    int anotherUselessField;};

and an array

struct SomeStruct array[10];

Then if you look at all the someFields in this array, they can be considered an array on their own, but they're not occupying consequent memory cells, so this array is strided. A stride here is sizeof(SomeStruct), i.e. the distance between two consequent elements of the strided array.

A sparse array mentioned here is a more general concept and actually a different one: a strided array doesn't contain zeroes in skipped memory cells, they're just not the part of the array.

Strided array is a generalization of usual (dense) arrays when stride != sizeof(element).


If you want to operate on a subset of a 2D array, you need to know the 'stride' of the array. Suppose you have:

int array[4][5];

and you want to operate on the subset of the elements starting at array[1][1] to array[2,3].Pictorially, this is the core of the diagram below:

+-----+-----+-----+-----+-----+| 0,0 | 0,1 | 0,2 | 0,3 | 0,4 |+-----+=====+=====+=====+-----+| 1,0 [ 1,1 | 1,2 | 1,3 ] 1,4 |+-----+=====+=====+=====+-----+| 2,0 [ 2,1 | 2,2 | 2,3 ] 2,4 |+-----+=====+=====+=====+-----+| 3,0 | 3,1 | 3,2 | 3,3 | 3,4 |+-----+-----+-----+-----+-----+

To access the subset of the array in a function accurately, you need to tell the called function the stride of the array:

int summer(int *array, int rows, int cols, int stride){    int sum = 0;    for (int i = 0; i < rows; i++)        for (int j = 0; j < cols; j++)            sum += array[i * stride + j];    return(sum);}

and the call:

int sum = summer(&array[1][1], 2, 3, 5);


To stride is to "take long steps"

thefreedictionary.com/stride

For an array this would mean that only some of the elements are present, like just every 10th element. You can then save space by not storing the empty elements in between.

A dense array would be one where many, if not all, elements are present so there is no empty space between the elements.