Problem arises when you don’t get to know why my program dysfunctional , inspite of correct logic . The problem may be with memory handling or in the thread handling . Learning syntax is half part , gaining proficiency is another part .
What exactly is memory management ?
In C/C++ assigning memory & handling threads can PITA until one reach a level of experience .
Let’s go by an example:
The above code has two errors . It doesn’t pops up during compilation and during execution .
First one : assigning out of bound index of array_alloc a value i.e array_alloc[20]
Second one : not freeing the allocated memory resulting in memory leak .
###Valgrind to rescue !
By definition : The Valgrind tool suite provides a number of debugging and profiling tools that help you make your programs faster and more correct. The most popular of these tools is called Memcheck. It can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredictable behaviour.
To rescue onself run the executable using valgrind command
The result is as following
There are two errors : Heap error for invalid assign & Leaks relating lost memory .
Valgrind can also be misused to profile your code (pun intended )
Further Valgrind can also be used for Bounty programs (like google chrome).
Conclusion !
There are other tools for debugging like GDB which is very elaborate & pro-like . Apart from that Valgrind is there .
As pointed out what’s better than finding out bugs as quickly without any pain .