Altera Nios II C2H Compiler User Manual

Page 67

Advertising
background image

Altera Corporation

9.1

3–27

November 2009

Nios II C2H Compiler User Guide

C-to-Hardware Mapping Reference

Although an array is considered to be a pointer type, dereferencing an
array variable does not always mean the same thing as dereferencing a
pointer. For example, dereferencing or indexing once into a
multidimensional array returns a pointer to the first element in another
array. For an N-dimensional array, a dereference or index into any of the
first (N-1) dimensions does not read a value from the array memory; it
computes an offset from the array's base address to determine the address
of a subsection of the array.

Example 3–18

demonstrates that indexing to the first level of a two-

dimensional array does not result in a memory access.

Example 3–18. Indexing a Multidimensional Array without Causing a Memory Access

char a[LENGTH][WIDTH]; // Here's a two-dimensional array
// The following assignments are all equivalent.
char *subscripting = a[3];
char *dereferencing = *(a + 3);
char *offset = (char *) (a + 3);
char *ptr_arithmetic = (char *) ((void *)a + 3*WIDTH);

Indexing into any of the first (N-1) dimensions of an N-dimensional array
requires a multiplication operation, as demonstrated by the evaluation of

ptr_arithmetic

in

Example 3–18

. If the size of the resultant array is an

integer power of two, then the multiplication operation is reduced to a
constant-shift operation, which does not require a hardware multiplier.
(Refer to section

“Unregistered Operations and Assignments” on

page 3–3

.)

A series of subscript operations that index into all N dimensions of an N-
dimensional array is equivalent to an indirection operation, which creates
an Avalon-MM master port.

Example 3–19

illustrates several cases that

generate an Avalon-MM master port to dereference an array variable.

Example 3–19. Indexing an Array and Causing a Memory Access

char a[LENGTH][WIDTH]; /* a is a two-dimensional array */
// The following assignments are equivalent.
char *subscripting_a = a[3][2];
char *dereferencing_a = *(*(a + 3) + 2);

char b[LENGTH]; /* b is a one-dimensional array */
// The following assignments are equivalent.
char *subscripting_b = b[1];
char *dereferencing_b = *(b + 1);

Advertising