Is it safe to convert, say, from an unsigned char *
to a signed char *
(or just a char *
?
C++ – Conversion from unsigned to signed type safety
cunsigned
Related Topic
- C++ – Iteration over std::vector: unsigned vs signed index variable
- Java – Can we make unsigned byte in Java
- C++ – Signed/unsigned comparisons
- C++ – Easiest way to convert int to string in C++
- Python – Why is reading lines from stdin much slower in C++ than Python
- Java – Declaring an unsigned int in Java
- C++ – use a pointer rather than the object itself
- C# – System.Windows.Application.GetResourceStream returns null
Best Answer
The access is well-defined, you are allowed to access an object through a pointer to signed or unsigned type corresponding to the dynamic type of the object (3.10/15).
Additionally,
signed char
is guaranteed not to have any trap values and as such you can safely read through thesigned char
pointer no matter what the value of the originalunsigned char
object was.You can, of course, expect that the values you read through one pointer will be different from the values you read through the other one.
Edit: regarding sellibitze's comment, this is what 3.9.1/1 says.
So indeed it seems that
signed char
may have trap values. Nice catch!