Arithmetic pointers and signed / unsigned conversions!

Is turning on pointer arithmetic integers automatically converted to their signed variants? If so, why?

Suppose i

int *pointer; 
int *pointerdiff;
unsigned int uiVal = -1;

pointerdiff = pointer + uiVal // Pointer will contain valid address here.

where the pointer is a pointer to int, and uiVal is initialized to -1, then I find that the address in the pointers decreases by 4. Why is the value unsigned -1 not considered here?

+3
source share
3 answers

It looks like your pointer is full.

. , 32- , 0x 12 34 56 78. int -1, 0x FF FF FF FF. , -1 4 294 967 295.

- (int*), sizeof int, 4 x86. , 0x 03 FF FF FF FC ( 0x FF FF FF FF * 4).

.

  0x 00 12 34 56 78
+ 0x 03 FF FF FF FC
-------------------
  0x 04 12 34 56 74

, , 40- , - 32 . 04 . 0x 12 34 56 74, 0x 12 34 56 78 - 4.

+8

, . . uiVal -1, . ,

unsigned int uiVal = -1;
pointer + uiVal ...

,

pointer + UINT_MAX ...

, , undefined, , , . 5.7/4

nonarray , .

, , , . , , [...]. , , ; , undefined.

, , , , undefined.

+3

, ptrdiff_t, , , .

0

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


All Articles