C++ Logo

std-proposals

Advanced search

Re: Allowing access to object representations

From: Jake Arkinstall <jake.arkinstall_at_[hidden]>
Date: Thu, 22 Aug 2019 08:17:38 +0100
On Thu, 22 Aug 2019, 02:06 sdkrystian via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> > It does. But the char pointer points to the first byte. Which is
> platform dependant.
>
> Don't think so: "the pointer value is unchanged" means exactly what it
> says, it still points to the int object. It is not specified anywhere that
> it will point to any char object.
>
> > Which is a char.
>
> No, it is never specified that it points to any char object. It points to
> an int object.
>

You have a pointer some int. That int is multiple char widths long. You
create a pointer to a char and reinterpret cast the int pointer into a
pointer to char. It might truly be a pointer to int, but by this point
there is no way for any code accepting that char pointer to know that it is
truly a pointer to int, char, or an elephant, but that, for all intents and
purposes, it can be treated as a char. Dereferencing the char pointer
grants you one char representing the value of the leftmost byte of the
integer.

What in the above is incorrect?

Im asking specifically because what you're saying directly contradicts an
example provided at the bottom of
https://en.cppreference.com/w/cpp/language/reinterpret_cast, where a char
pointer to an int is dereferenced to determine the endianness of the system.



>

Received on 2019-08-22 02:19:49