4.3.6.2.1 Pointers to Both Memory Spaces
Any pointer to const
has the ability to read objects in
both the data and program memory space. Such pointers are said to be mixed memory space
pointers and they may be larger than regular data pointers defined without using
const
on the target type.
For example, the following function can read and return an
int
from either memory space, depending on the address passed to
it.
int read(const int * mip) {
return *mip;
}
For any device in the avrtiny or avrxmega3 families, these pointers are 16-bits wide and can target objects in data or in program memory, since the program memory is mapped into the data space. For other devices, the pointers are 24 bits wide.
Addresses of type const *
that are 24-bits wide
linearize flash and RAM, using the high bit of the address to determine which memory
space is being accessed. If the MSb is set, the object is accessed from data memory
using the lower two bytes as address. If the MSb of the address is clear, data is
accessed from program memory, with the RAMPZ segment register set according to the high
byte of the address.
Note that the pointer type const int *
is similar to
the const __memx int *
pointer type in terms of how it accesses
objects, but it does not require the use of non-standard keywords, and access is
controllable using the -mconst-data-in-progmem
option (see 3.6.1.5 Const-data-in-progmem Option).