Question: Unlike Java, in C/C++ the following is **allowed**:

```
int* foo ()
{
  if(x)
    return p;

  // What if control reaches here?
}

```

This often causes crashes and it is hard to debug problems. Why doesn't the *standard* enforce to have a **final return** for non-`void` functions? (Compilers generate an error for a wrong `return` value.)

Is there a flag in GCC or [MSVC](https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B) to enforce this? (something like `-Wunused-result`)

Answer: 
Use the [-Wall](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall) flag in [GCC](https://en.wikipedia.org/wiki/GNU_Compiler_Collection).

> 
> warning: control reaches end of non-void function
> 
> 
> 

Or more specifically, [-Wreturn-type](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wno-return-type).
What the standard says about this kind of programming is that it produces *undefined behavior*.

Undefined behavior is the joy and pity of C/C++, but it is also a fundamental feature of the language design that allows for many of the low-level optimizations that make C a sort of "high level assembler" (it is not, actually, but just to give you an idea).

So, while redirecting to John's answer about the switch to use with GCC, to know "why" the standard does not prevent that, I would point to a very interesting analysis of undefined behavior and all of its misteries: [What Every C Programmer Should Know About Undefined Behavior](http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html). It makes for a very instructive reading.