how does delete() work?

Discussion in 'C' started by tom_ge, Mar 20, 2007.

  1. tom_ge

    tom_ge New Member

    Joined:
    Mar 20, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    delete() in C take a void pointer as an argument.How does it know how many bytes of memory to delete ? Can anyone help me out ?

    Thanks in advance
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    One word answer is magic and the more detail answer is it stores the amount allocated when allocating. It depends on the compiler used and as far as I remember MS VC stores before the memory chunk. Dont quote me on this as I dont have the VC installed on this PC.
     
  3. tom_ge

    tom_ge New Member

    Joined:
    Mar 20, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Thanks shabbir,

    So am i right in saying
    if i allocate a character pointer with 100 bytes of memory..in reality there is 100 bytes + 4 bytes(initial block for pointer to know how many bytes internally allocated to the pointer) allocated, but these 4 bytes are not acccessible by the user and is only for compiler to know how many bytes to delete.
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    There are more than 4 bytes of overhead, but you're getting the picture. You can google for malloc implementations if you want the precise details for your particular implementation. The delete function is actually a C++ thing, not a C thing. New and delete are wrappers for the C-type malloc (and family) and free operations.
     
  5. tom_ge

    tom_ge New Member

    Joined:
    Mar 20, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Thanks Dawei
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice