Let's take this piece of code:
node_t* create_tree_node(void* data){
node_t* node = (node_t*) malloc(sizeof(node_t));
node->data = data ;
node->left = NULL ;
node->right = NULL ;
}
Now, If you noticed, there's no return statement, let's say we forgot to put one.
I actually tried a similar code (imagine node_t
being the habitual structure for a node in a binary tree, ie, a field for the data, and pointers to the next left and right nodes), and, when I did this:
node_t* node = create_tree_node((void*) some_random_value) ;
I was able to create a node with the desired value , and save it in a binary tree, use it, and free it.
So two question:
- what does a non void function without any return statement actually return ? Could it be the last allocated variable on the stack ?
- GCC raises a warning for that, but not an error. Wouldn't a compiler error be actually more pertinent for such a case instead of a compiler warning?