Invalid conversion between named address space pointers - pointer assignment
0 reputation · 02 Sept 2020, 10:17 UTC
0 reputation · 02 Sept 2020, 10:17 UTC
A design goal is to place infrequently accessed data in a named address space mapped to cheaper memory to reduce BOM and runtime power for a low-traffic workload. Embedded C named address spaces require distinct pointer types, and an implicit assignment from a named address space qualified pointer to a generic pointer is a constraint violation.
The constraint is documented for compliant compilers, where the conversion is diagnosed as invalid conversion between named address space pointers unless an explicit cast is used. This affects code that moves data between address spaces and any interface that expects plain pointers.
The specification leaves the behavior of standard library functions with named address space pointers unresolved. It is not defined how functions such as memcpy, memmove, or strcpy handle source and destination pointers that carry named address space qualifiers.
What is the defined requirement for pointer conversions between named address spaces and generic pointers in Embedded C? Does the standard require implementations to diagnose or reject calls to standard library functions when arguments are named address space pointers? Which implementations define the behavior for mixed address space copies, and how is portability assessed?
34775 reputation · 02 Sept 2020, 15:42 UTC
Under the Embedded C address-space model, an implicit conversion between a pointer to a named address space and a generic pointer, in either direction, is a constraint violation. A conforming implementation must emit a diagnostic for assignments such as:
__address_space(__mem) int *src;
int *dst = src; /* invalid: implicit named-address-space conversion */
The permitted fix is an explicit cast:
int *dst = (int *)src;
The cast removes the diagnostic, but the pointer value itself remains implementation-defined unless the toolchain documents how named and generic address spaces relate.
Confirmed: the constraint-violation rule applies to pointer assignment, initialization, parameter passing, and return values when the types differ only by named address-space qualification.
Open: the Embedded C technical report does not define how memcpy, memmove, strcpy, or related functions treat named-address-space pointers. The standard therefore does not require an implementation to diagnose or reject those calls. Each toolchain chooses one of these approaches:
Portable code should not depend on undocumented library acceptance of named-address-space pointers. Use explicit casts to generic pointers for standard library calls, or isolate address-space-specific copies behind vendor-specific wrappers. Relying on implicit library support makes the program non-portable across compilers that implement the Embedded C extensions minimally.
Which embedded compiler and version are you targeting? This determines whether a cast is sufficient for library calls or whether a vendor-specific copy routine is required.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 02 Sept 2020, 12:07 UTC
The C18 standard treats a pointer qualified with a named address space as a distinct type. Implicitly converting such a pointer to a generic void * or to a pointer in another address space is a constraint violation – a conforming compiler must issue a diagnostic if the conversion is attempted without an explicit cast.
Prototypes like memcpy and strcpy are defined only for void * or unqualified object pointers. Passing a named‑address‑space pointer to them yields undefined behavior; the standard does not require the library to detect or reject such calls.
__builtin_memcpy in GCC/Clang) that accept address‑space qualifiers and generate the correct hardware instructions.These practices keep code portable across GCC, Clang, and vendor toolchains while satisfying the standard’s diagnostic requirements.