NLinker icon

C++ UB

NLinker | PRO | 02/18/19 02:03:23 AM UTC | 0 ⭐ | 1054 👁️ | Never ⏰ | []
C++ |

730 B

|

None

|

0 👍

/

0 👎

// by @MikailBag
// the simple code compiles without any warnings under -Wall BUT
// ❯❯❯ ./test
// [1]    12892 segmentation fault (core dumped)  ./test
 
// However with Wextra it warns:
// ❯❯❯ gcc -Wextra -o test test.c
// test.c:16:1: warning: missing initializer for field ‘f3’ of ‘struct Foo’ [-Wmissing-field-initializers]
//  };
//  ^
// test.c:7:12: note: ‘f3’ declared here
//      void (*f3)(void);
//             ^~
 
#include <stdio.h>
struct Foo {
    void (*f1)(void);
    void (*f2)(void);
    void (*f3)(void);
};
 
void g1(void) { printf("g1"); }
void g2(void) { printf("g2"); }
 
static struct Foo foo = {
    g1,
    g2
};
 
int main() {
    foo.f1();
    foo.f2();
    foo.f3();
}

Comments