-1

What happens to the memory which is allocated using malloc() and is not freed using free()?

Will it be considered a memory leak?

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    possible duplicate of [What REALLY happens when you don't free after malloc?](http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc) – devnull Jun 28 '13 at 11:32

3 Answers3

3

If you continue to allocate memory in a long running program, and never free it even after you are done with it, then yes that's a leak. If it's a short program then it might be okay as most modern operating systems free that memory on process termination.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Yes it can be considered as memory leak. You should always free dynamically allocated memory. Consider a situation where your program uses alot of memory, which is being allocated at different steps. Now if you keep freeing memory which you don't need anymore you may not face any trouble but if you don't free memory you may run out of memory and your program may get terminated. Hence it is considered a good practice to always free memory otherwise it will keep accumulating and hamper your program's performance

0

Calling malloc many times without freeing just fragmenting the memory to the point that the largest block is equal to the total available.
Any memory request can fail at any time, and your code needs to be able to control that.

boleto
  • 1,149
  • 1
  • 22
  • 32