LINUX.ORG.RU

История изменений

Исправление EXL, (текущая версия) :

$ cat seg.c 
int* a();

int main() {
    int* x = a();
    int b = *x;
    printf("%d\n",b);
}

int* a() {
    int x= 25;
    return &x;
}


$ gcc seg.c 
seg.c: In function ‘main’:
seg.c:6:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
    6 |     printf("%d\n",b);
      |     ^~~~~~
seg.c:6:5: warning: incompatible implicit declaration of built-in function ‘printf’
seg.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
  +++ |+#include <stdio.h>
    1 | int* a();
seg.c: In function ‘a’:
seg.c:11:12: warning: function returns address of local variable [-Wreturn-local-addr]
   11 |     return &x;
      |            ^~


$ ./a.out 
Segmentation fault (core dumped)

https://stackoverflow.com/a/12380788
https://stackoverflow.com/a/14872593

Исходная версия EXL, :

UB

$ cat seg.c 
int* a();

int main() {
    int* x = a();
    int b = *x;
    printf("%d\n",b);
}

int* a() {
    int x= 25;
    return &x;
}


$ gcc seg.c 
seg.c: In function ‘main’:
seg.c:6:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
    6 |     printf("%d\n",b);
      |     ^~~~~~
seg.c:6:5: warning: incompatible implicit declaration of built-in function ‘printf’
seg.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
  +++ |+#include <stdio.h>
    1 | int* a();
seg.c: In function ‘a’:
seg.c:11:12: warning: function returns address of local variable [-Wreturn-local-addr]
   11 |     return &x;
      |            ^~


$ ./a.out 
Segmentation fault (core dumped)