What is the difference between free and delete?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ycinar
    New Member
    • Oct 2007
    • 39

    What is the difference between free and delete?

    Say we have a pointer like below

    Code:
    Test * abc = GetValue();
    // do something
    free (abc);
    // or 
    delete abc;
    does it matter if you chose delete or free to avoid memory leaks?

    thanks
  • zonar00
    New Member
    • Oct 2007
    • 16

    #2
    Originally posted by ycinar
    Say we have a pointer like below

    Code:
    Test * abc = GetValue();
    // do something
    free (abc);
    // or 
    delete abc;
    does it matter if you chose delete or free to avoid memory leaks?

    thanks
    as far as i know the ans lies on how these two functions are used independently of each other.like free only ensure that the space oocuiped by the varaible is made re-initalized (emptied) and is available for further use. Like
    {12,222,334,34} ={0,0,0,0)

    where as delete would delete the entire memory space occupied making it imposible to refer the varaiable once its gets deleted where in free u could still access it.

    Comment

    • mac11
      Contributor
      • Apr 2007
      • 256

      #3
      free and delete both do the same sort of thing - that is, reclaim dynamically allocated memory. There are significant differences but the quick and dirty rule is to use free to deallocate memory which you allocated using malloc - and to use delete to deallocate memory which you've allocated using new.

      you can google for "c++ free" and "c++ delete" for all the gory details -there is no need for me to type them out here.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        It's as simple as this.

        If the memory the pointer points to was allocated with malloc(), calloc(), or any function like that, you must use free().

        If the memory the pointer points to was allocated with the new operator, you must use delete.

        If you are working in C++, there is no reason to use malloc(), calloc(), realloc(), etc. so there should be no reason to use free().

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          And further more,delete calls a object destructor(if allocated variable is a object)
          which deallocates memory used by object members(if there is some memory,and if you have coded the destructor well).Similar thing is with new,it calls object constructor...( etc.)

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            And further, malloc() and free() use the C Run-time heap (CRT heap). Whereas new and delete may be overloaded on a class basis to use a private heap.

            Private heaps are cool since you just delete the heap when you are done and that avoids the leaks, period.

            So, you could new an object, use free() to release heap allocations and the free() would crash the program since the allocations weren't made in the CRT heap.

            malloc() and free() are part of C. C++ is not C. Do not use these things.

            Comment

            Working...