Thread: Do you allways need to use free after malloc?

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    1

    Do you allways need to use free after malloc?

    I see alot of tutorials using malloc, calloc for allocating memory space, but they are not freeing up the memory.
    Shouldn't free be used after malloc or calloc is been used?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,120
    It's generally a good habit to free memory after you're done with it. So you're more likely to do the right thing when it actually matters.

    If your program is short-lived, and mallocs what it needs at the start, there is seldom any need to call free.
    Question 7.24

    But if you're writing a long running server or daemon which is expected to run indefinitely, you need to make sure you free everything when you're done with it.
    Otherwise, you'll slowly leak memory until the OS eventually kills the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  2. malloc() and free()
    By rlesko in forum C Programming
    Replies: 5
    Last Post: 12-05-2010, 01:49 PM
  3. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  4. gethostbyname allways NULL
    By BianConiglio in forum Windows Programming
    Replies: 6
    Last Post: 08-18-2005, 01:27 AM
  5. Allways full screen
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-01-2002, 04:15 AM

Tags for this Thread