Advertisement
NLinker

C++ UB

Feb 17th, 2019
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. // by @MikailBag
  2. // the simple code compiles without any warnings under -Wall BUT
  3. // ❯❯❯ ./test
  4. // [1]    12892 segmentation fault (core dumped)  ./test
  5.  
  6. // However with Wextra it warns:
  7. // ❯❯❯ gcc -Wextra -o test test.c
  8. // test.c:16:1: warning: missing initializer for field ‘f3’ of ‘struct Foo’ [-Wmissing-field-initializers]
  9. //  };
  10. //  ^
  11. // test.c:7:12: note: ‘f3’ declared here
  12. //      void (*f3)(void);
  13. //             ^~
  14.  
  15. #include <stdio.h>
  16. struct Foo {
  17.     void (*f1)(void);
  18.     void (*f2)(void);
  19.     void (*f3)(void);
  20. };
  21.  
  22. void g1(void) { printf("g1"); }
  23. void g2(void) { printf("g2"); }
  24.  
  25. static struct Foo foo = {
  26.     g1,
  27.     g2
  28. };
  29.  
  30. int main() {
  31.     foo.f1();
  32.     foo.f2();
  33.     foo.f3();
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement