FAQ - Recursive APIs - why I never use them
FAQ - Recursive APIs - why I never use them

When you learn programming usually you learn about various programming constructs. One of them is about calling APIs (functions) recursively. Recursive function calling is quite common in situations such as linked-lists traversal, tree data-structures, sorting algorithms (such as quick sort, merge sort, etc), divide-and-conquer situations and so on.

But I recommend to strictly stay away from recursive function calling. Since in certain situations it can lead to infinite looping and although it is somewhat ok in user-space programming, strictly not recommended in Kernel programming. Since if there is a long loop in kernel flow, that can lead to serious performance issues. And any crash in kernel can bring-down the whole system. And even in user-space if there any run-away loop then it can cause the process (application) to fail.

To make matters worse, if you do any allocations (kmalloc/malloc) in every iteration of recursive APIs, then if you fail to cleanup such an allocation again it can lead to memory leaks. So rather than skill to use recursive functions, its better you STRICTLY AVOID all together such a programming practice. But unfortunately in colleges when they teach students, they never warn its several drawbacks which can occur in real production like situations. The last thing you need in production code is unpredictable, hard-to-debug, difficult to maintain code vs a simple to understand straightforward code.

Here is my detailed YouTube video on recursive APIs and the reasons why I never prefer to use them:

I also conduct sessions/classes on Systems and Network Software Programming, Linux Kernel Programming and Architecture. If you are interested, click HERE for more details.

If you have any queries or anything to discuss further on Linux Kernel Programming and writing Kernel modules kindly feel free to contact me.