10

From the C++ standard:

5.2.10.3

The mapping performed by reinterpret_cast might, or might not, produce a representation different from the original value.

I've been trained at this very site to believe and repeat this. (Even if it's possibly just trivia). A reinterpret_cast from float* to int* is allowed to produce a different bit pattern. The only guarantee is that reinterpret_cast-ing that result back to float* will produce the original bit pattern.

My question: Does this ever happen? Is there an existing, real-world platform or CPU or compiler that actually reinterpret_casts to a different bit pattern? If not, are there any real-world situations where reinterpret_cast has any runtime overhead?

In all my experience with reinterpret_cast, the cast was a directive to the compiler, not the runtime.

1
  • 3
    If there isn't such a platform, then this question will be hard to answer, as I doubt there are people who can confidently say "there is no such platform, I know them all". However, as a purely hypothetical example, there could be a machine, which requires N-byte memory alignment, and has spare bits in pointer values, and would use those bits to encode some extra information, different for float* and int*. So if standard wouldn't allow changing bit pattern, the cast would be impossible on that architecture.
    – hyde
    Jul 9, 2014 at 5:46

2 Answers 2

5

Pointers can in principle be different sizes. The largest pointer, if there is any difference (disregarding member pointers, talking about real pointers), is char*, since a char by definition is a byte and can be anywhere, no alignment. void* must be able to represent char*.

On a system with int* using fewer bits than char*, reinterpret-casting in that direction might be a bit risky.

I think with these pointers (heh) you can find this in the standard. It's the requirement about void* being large enough for any pointer, and the thing about alignment: the stricter/larger, the fewer bits needed for pointer to that type. But I have not ever heard of any extant system where there is such difference.


Standardese regarding void* being able to represent char*:

C++11 §3.9.2/4:


A pointer to cv-qualified (3.9.3) or cv-unqualified void can be used to point to objects of unknown type. Such a pointer shall be able to hold any object pointer. An object of type cv void* shall have the same representation and alignment requirements as cv char*

The “any object pointer” implies vaguely that there are different sizes of pointer.


Standaredese regarding the alignment of referent:

C++11 §5.2.10/7:


An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast<cv T2*>(static_cast<cv void*>(v)) if both T1 and T2 are standard-layout types (3.9) and the alignment requirements of T2 are no stricter than those of T1, or if either type is void. Converting a prvalue of type “pointer to T1” to the type “pointer to T2” (where T1 and T2 are object types and where the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value. The result of any other such pointer conversion is unspecified.

It’s worth noting that later on in the standard there is some support for C-style emulation of class derivation, that apparently contradicts the “any other” at the end above:

C++11 §9.2/20,


A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa.

In this case the two objects necessarily have the same alignment, while the previous cited paragraph talked only about alignment of types – but clearly the formal little contradiction is not a practical problem, as I see it.

1
  • 2
    I participated in a discussion about the Mill CPU where one of the team member said that Ivan Godard was working on a "floating pointer" representation which would allow to detect some out of bounds accesses. In this case, the inner representation (in the CPU) of the pointer would make use of the unused bits to express the range (somehow). It was unclear in the discussion how far they were, and whether this would be accessible outside the CPU, but at the very least they are thinking about it. Jul 9, 2014 at 6:54
1

I've worked on platforms where char* was larger than int*, and on platforms where they had a different layout, even though the size was the same. Neither of the machines in question are particularly relevant today, however (although the second, the PDP-10, was one of the more important machines in its heyday). It's also conceivable that some compilation modes on an Intel in native mode (or what used to be called native mode) would "normalize" pointers in a reinterpret_cast, or even an implicit conversion, in order to facilitate address comparisons. It's also conceivable (although I've not seen it) that such conversions enforce correct alignment, e.g. a conversion from char* to int* might force the 2 low-order bits to 0. In practice, however, I don't think, today, that you're likely to see a reinterpret_cast make any changes between data pointer types. The issue is more historic. (But I'm not sure about modern embedded processors. From what I understand, many of them are word addressing, and so if sizeof(int) != sizeof(char), they're likely to need a special format to address char.)

4
  • In my limited experience microcontrollers do not change the pointers and instead crash with a bus error when a misaligned pointer is dereferenced. That probably has something to do with being unable to do the transformation with no runtime cost.
    – nwp
    Jul 9, 2014 at 9:42
  • 1
    For these situations you mention, "char* is larger than int*", "native mode pointers must be normalized", "low-order bits must be zeroed" - does that mean that my premise in the question about round trip casting having to produce "the original bit pattern" is wrong? Jul 9, 2014 at 10:10
  • @nwp This is the behavior of Sun CC or g++ on a Sparc; it's probably the universal behavior on a machine with byte addressing. But do all embedded processors have byte addressing? What about DSPs? That's where I'm not sure. A compiler trying to implement 8 bit bytes on a 32 bit DSP without byte addressing will have to put the additional bits to select the byte somewhere outside the normal hardware address. Jul 9, 2014 at 10:14
  • @DrewDormann No. Round trip is only guaranteed if the intermediate type has less alignment requirements than the end types; this is not the case for char*->int*->char*. And for int*->char*->int*, the bits that will be forced to 0 in the intermediate char* will be 0 anyway. Jul 9, 2014 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.