I am not from a computer science background and have been trying to learn Data Structures using C programming.
I have made some binary tree programs and realised that I have used malloc() left and right (pun intended :)) without ever using free() because I don't know how many nodes I have actually created.
My question is rather basic and almost stupid. Do I have to write a function similar to the print function to visit each node of the tree and free it individually or just free(root) will be enough since all the subsequent pointers will be lost anyway?
malloccall, you should have a correlativefreecall (with the address you got from thatmalloc).my_malloc()function to dole out pieces of that huge memory block. Then when finished, you only need to free that one huge block you allocated in the beginning. It's arguably very bad practice and having a very bad motive, but it's great to know there is always another way to do something in C, and possibly a great learning experience as you work out the code for it.