Wednesday, 26 December 2018

How we can prevent memory leak in C Programming?

C Programming can manage its stack memory. But it does not track your use of the heap. You have to do that. For every malloc () you do, have a corresponding call to free (), when you are done with the memory.



The general idea is everything which you’ve allocated dynamically (i.e. through something like malloc) is your program’s responsibility to release (i.e. free). Anything it doesn’t free is going to be a memory leak - i.e. as the program keeps running it may be allocating new parts, but it “forgets” to release them once it’s done with those parts.

Whenever you use dynamic allocation (you call malloc or calloc), you need to: 1st check if the returned pointer is not NULL and if not then, 2nd, call free when you no longer need the allocated memory. You must make sure that, until that time, you do have access to the pointer (you don’t “forget” the allocated memory address).

To learn more about C Programming Language

visit us @ http://tccicomputercoaching.com/course/

TCCI coaching institute is located in Bopal and Satellite in Ahmedabad. We teach various courses relavant to College, Universities and School (All Board). We provide Online Coaching for all computer courses.

You can join Mathscourse(School-Uvivercities) and all Engineering Branch at TCCI.

Call us @ 9825618292

Visit us@ http://tccicomputercoaching.com/programming-course/

No comments:

Post a Comment