Reinterpret_cast between ponter and different size intgeral type

What actually happens when we do the following: 1)

int i = -1; // 32 bit
void *p;
p = reinterpret_cast<void*>(i) 

in 64-bit architecture, sizeof (void *) == 8

2)

long long i; // 64 bit
void *p = (unsigned int)(-1);
i = retinterpret_cast<long long>(p) 

in 32bit architecture sizeof (void *) = 4

I generally understand what will be the result, but I want someone to describe the mechanism in terms of the C ++ standard for a better understanding.

In the second case, the behavior is similar to the behavior described in "integral stocks" (4.5) (i will be -1) for the case of "int - unsigned long", so do we usually say that pointers are converted as signed integers?

3)

int i = ...;
unsigned long long L = ...;
i = L;

What rules apply here?

+4
source share
2 answers

. , :

. ( )

, , ( ), :

, , .

, , int , int, ( ) ; , - . , , : 0. , , "".

: , void* . intptr_t ( ); . , . , , . , , . ( 40 50. MSC, g++ Sun CC, , , Mainstream Unix, . 16- Intel, , , , , .)

, : . , , , , - , , . , , ( , , ), , .

EDIT:

, unsigned long long int. , a reinterpret_cast, . , : : " ":

, , ( ); .

C , . ( , , "" ++ , .) : , , .

+5

Undefined. . , . reinterpret_cast<void*>(1) == reinterpret_cast<void*>(1) false.

-1

Source: https://habr.com/ru/post/1546991/


All Articles