In C, is it guaranteed that the starting address of the array is less than the addresses of other elements?

In other words, when executed

index = &array[x] - &array[0];

Is it always guaranteed (by the C standard) that & array [0] <= & array [x], or depends on the compiler? What are the main chapters of C related to this topic?

+7
source share
7 answers

Ordering address is guaranteed. The behavior of relational operators is defined in C11 6.5.8p5 :

[...] pointers to elements of an array with large index values ​​lower than pointers to elements of the same array with lower index values. [...]

, &array[x] >= &array[0] , x , . ( x , .)

&array[x] - &array[0] ,

  • x - ,
  • x , PTRDIFF_MAX

angular : C11 6.5.6p9 ,

9 , ; . , ( ) - ptrdiff_t, <stddef.h>. , . , P Q i- j- , (P) - (Q) ij, ptrdiff_t.[...]

ptrdiff_t , size_t, , x, , PTRDIFF_MAX; &array[x] >= &array[0] , &array[x] - &array[0] .


. x86-64 64- Ubuntu Linux, 32- . 32- X86 Linux + GCC ptrdiff_t - 32- , size_t - 32- . , 64- Linux 32- , 2 malloc, 4 .

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stddef.h>

int main(void) {
    size_t size = (size_t)PTRDIFF_MAX + 2;
    size_t x = (size_t)PTRDIFF_MAX + 1;
    char *array = malloc(size);
    if (! array) {
        perror("malloc");
        exit(1);
    }
    array[0] = 42;
    array[x] = 84;
    printf("&array[0]: %p\n", (void *)&array[0]);
    printf("&array[x]: %p\n", (void *)&array[x]);
    printf("&array[x] >= &array[0]: %d\n", &array[x] >= &array[0]);
    printf("&array[x] - &array[1]: %td\n", &array[x] - &array[1]);
    printf("&array[x] - &array[0]: %td\n", &array[x] - &array[0]);
    printf("(&array[x] - &array[0]) < 0: %d\n", (&array[x] - &array[0]) < 0);
}

32- :

% gcc huge.c -m32 -Wall && ./a.out 
&array[0]: 0x77567008
&array[x]: 0xf7567008
&array[x] >= &array[0]: 1
&array[x] - &array[1]: 2147483647
&array[x] - &array[0]: -2147483648
(&array[x] - &array[0]) < 0: 1

, - 0x77558008, &array[x] - 0xf7504008, &array[x] , &array[0]. &array[x] - &array[1] , &array[x] - &array[0] !

+12

, FWIW, C11, §6.5.6/P9, (emphsis mine)

, , ; . [...]

, (). (.. - |a-b|)


, "" ( <, >, <=, >=), :

, . [....] , , , [...] , . [....]

, &array[x] <= &array[0] 0 (FALSY), x > 0.

+3

, &array[x] array+x.

6.5.2.1p2:

, [] . [] , E1 [E2] (* ((E1) + (E2))). - , + , E1 - ( ), E2 - , E1 [E2] E2- E1 ( ).

+2

C11 , () . :

, , ; .. , type ( ) ptrdiff_t, . , undefined. , P Q i- j- , (P) - (Q) i-j, ptrdiff_t. , P , Q , ((Q) +1) - (P) , ((Q) - (P)) + 1 as - ((P) - ((Q) +1)) 0, P , (Q) +1 .

, x - 0.

+2

C11 (ISO/IEC 9899: 2011 (E)) §6.5.8/5:

,... , , ,... , .

, &array[x] <= &array[0] false, x .

+2

, , , .

char[] foobar;
char *foobarPtr = foobar;

foobar[0] == *foobarPtr++;
foobar[1] == *foobarPtr++;

https://www.tutorialspoint.com/cprogramming/c_pointer_to_an_array.htm

0
index = &array[x] - &array[0];

-

index = (array+x) - (array+0)

C .

, pointer arithmetic, index = x

, ISO9899, ​​ pointer arithmetic .

0

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


All Articles