C is weird.

Published Dec 26, 2013

Falling off the end of a function that is declared to return a value (without explicitly returning a value) leads to undefined consequences.


Yeah. I forgot to write a return line in a function that was supposed to return a pointer. I didn’t notice it for days since the program worked fine – the right thing was being returned!

I tried it out on my BeagleBone Black and it looked like calloc() was returning NULL. Weird! Then I noticed that I wasn’t actually returning anything.

It’s interesting that these platforms have different results.

Here’s my oops.

Apparently the program put the result of calloc() onto the register used for the return value on x86! Neat!