📜 ⬆️ ⬇️

Once again about the indefinite behavior or "why you should not hammer nails with a chainsaw"

. , , , , , , : - — , , , , , .

- , , , , . , «» : « … , , , , … ?».

, , , , , , -, , , , «» , , , .

?

: .

, , . . ( Java, ). ( ) , , . - (, — UNIX, K&R — , , ANSI ISO — - ), - . ( ANSI C ISO C) .

. , -, - : UNIX. . , , , , UNIX — , .

- .

: - ? : , , , , , . , Java ECMAScript. , , . , - « » ( 1 129 x86 2, ARM' 0, «1 << 129» ) .. .. OS. — « » 10 ( 10, 100)?

UNIX ( ) . , UNIX — . ? . ? . , ( 8086)? . , «» ? . .. .. , .

: — , . , . — , , . , — , ? : , ! , UNIX ( « » Linux) . , , « » # Java. , C# Java , — , . , Java « ».

, UNIX ( : , ). , . .

: , «» ? , iAPX 432 . . begin end C++. — . , , «» . , : « », , , — . , — «» . , , , — «», — « ».

« ». , , int unsigned int. . : «, , - ». « »: ( , ), . , - . int unsigned int … -.
, , .

( ", aka "implementation-defined behavior") . ( " " aka "undefined behavior") : , , , . ( , , ).
aka unspecified behavior — ( f()+g() , - , ). — : .

, — - . , , . , ? : 8086 8087. , , . , , float int ( long 16- ) ! ( 8087) , 8086 «»!

: ( ), «»: int, int, float, float. , , — , ! , .

— ! , , ? p realloc' realloc' . , : , , « » - … , !

… , :
#include <stdio.h>
#include <stdlib.h>
 
int main() {
  int *p = (int*)malloc(sizeof(int));
  int *q = (int*)realloc(p, sizeof(int));
  *p = 1;
  *q = 2;
  if (p == q)
    printf("%d %d\n", *p, *q);
}
$ clang -O realloc.c ; ./a.out 
1 2
… , , ? : realloc, , : p₁, p₂ q. p₂ , , q (, , , — ). *p₂ *q . — : *p₂ 1, q 2. printf. ! p₂ == q - false , — .

, , . , , « » , «». … — , «» .

, , : ( , - ) , — , C: , , , , . , . , … !

, ? : - ? ! , ! , : , , , , , — , . !

, . GCC : ( — ), -fwrapv, -ftrapv, . -fno-strict-aliasing, may_alias. . — , , , .

, , , : , , — - , 99% . — - , — .

')

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


All Articles