]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gcc/c-implementation-defined-behavior/arrays-and-pointers.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gcc / c-implementation-defined-behavior / arrays-and-pointers.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. _arrays-and-pointers-implementation:
7
8 Arrays and Pointers
9 *******************
10
11 * The result of converting a pointer to an integer or
12 vice versa (C90 6.3.4, C99 and C11 6.3.2.3).
13
14 A cast from pointer to integer discards most-significant bits if the
15 pointer representation is larger than the integer type,
16 sign-extends ([#f1]_) if the pointer representation is smaller than the integer type, otherwise
17 the bits are unchanged.
18
19 .. ??? We've always claimed that pointers were unsigned entities.
20
21 .. Shouldn't we therefore be doing zero-extension? If so, the bug
22
23 .. is in convert_to_integer, where we call type_for_size and request
24
25 .. a signed integral type. On the other hand, it might be most useful
26
27 .. for the target if we extend according to POINTERS_EXTEND_UNSIGNED.
28
29 A cast from integer to pointer discards most-significant bits if the
30 pointer representation is smaller than the integer type, extends according
31 to the signedness of the integer type if the pointer representation
32 is larger than the integer type, otherwise the bits are unchanged.
33
34 When casting from pointer to integer and back again, the resulting
35 pointer must reference the same object as the original pointer, otherwise
36 the behavior is undefined. That is, one may not use integer arithmetic to
37 avoid the undefined behavior of pointer arithmetic as proscribed in
38 C99 and C11 6.5.6/8.
39
40 * The size of the result of subtracting two pointers to elements
41 of the same array (C90 6.3.6, C99 and C11 6.5.6).
42
43 The value is as specified in the standard and the type is determined
44 by the ABI.
45
46 .. [#f1] Future versions of GCC may zero-extend, or use a target-defined ``ptr_extend`` pattern. Do not rely on sign extension.