What happens if your program never puts back the memory it borrows?
Why free function? - Purpose & Use Cases
Imagine you are managing a big box of toys. Every time you take a toy out, you need to remember to put it back in the box when done. If you forget, the toys pile up and clutter your room.
Without a clear way to put toys back, your room becomes messy. Similarly, in C programming, if you don't release memory you no longer need, your program uses more and more memory, slowing down or crashing.
The free function helps you put back the memory you took. It cleans up the space so your program stays fast and tidy, just like putting toys back keeps your room neat.
char *name = malloc(100);
// use name
// forget to free memorychar *name = malloc(100);
// use name
free(name);Using free lets your program run smoothly without running out of memory, even when handling many tasks.
Think of a video game that loads many levels and characters. Without freeing memory, the game would slow down or crash after a while. Using free keeps the game running fun and smooth.
Memory must be given back when no longer needed.
free helps prevent memory waste and crashes.
It keeps programs efficient and reliable.