DEV Community

Discussion on: I'm an Expert in Memory Management & Segfaults, Ask Me Anything!

 
ac000 profile image
Andrew Clayton

OK, firstly, a small nitpick, you weren't getting a segfault (SIGSEGV) but a SIGABRT

As for failing to return a value from a non-void function, you should at the very least compile with -Wall, which would have caught that. e.g

/* n.c - no return from non-void function */

static int test(void)
{
}
$ gcc -c n.c
$

vs

$ gcc -Wall -c n.c
n.c: In function ‘test’:
n.c:5:1: warning: no return statement in function returning non-void [-Wreturn-type]
    5 | }
      | ^
At top level:
n.c:3:12: warning: ‘test’ defined but not used [-Wunused-function]
    3 | static int test(void)
      |            ^~~~
$

And of course we also get the second warning...

I always compile with at least '-Wall -Wextra'