1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How the compiler differentiates delete[] and delete

Discussion in 'C' started by sharmila, Jun 16, 2006.

  1. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Hi all,
    To delete an array of objects we use delete[] and to delete an object we use delete.But how the compiler knows that in delete[] how many objects it has to delete?
    i.e
    if we write as
    char *c = new char[10];
    To delete this we use delete[].
    For char *c = new char;
    we use delete.
    Then how the compiler differentiates between these two.

    Thanks in advance,
    Regards,
    sharmila.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Short answer: Magic.

    Long answer: The run-time system stores the number of objects, n, somewhere where it can be retrieved if you only know the pointer, p.

    [comment]I have added the reference to the question in the [thread=699]Some C-C++ Programming tips[/thread] thread[/comment]
     
  3. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    It does not answer how the compiler knows if its how many element to delete in delete[] but it just says when you should be using delete[]
     
  5. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Hi shabbir,
    In that link it is menctioned that

    Typically, array new will insert information adjacent to the memory allocated for an array that indicates not only the size of the block of storage but also the number of elements in the allocated array. This information is examined and acted upon by array delete when the array is deleted.

    The format of this information is probably different from that of the information stored with a block of storage obtained through scalar new. If scalar delete is invoked upon storage allocated by array new, the information about size and element count—which are intended to be interpreted by an array delete—will probably be misinterpreted by the scalar delete, with undefined results. It's also possible that scalar and array allocation employ different memory pools. Use of a scalar deletion to return array storage allocated from the array pool to the scalar pool is likely to end in disaster.

    Regards,
    sharmila.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Actually thats not true. Allocate a pointer using
    int *i = new int[5];
    and then try seeing the memory around the allocated area (I saw that in VC++ 6) and you will not find that data anywhere.
     
  7. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Hi shabbir,
    We will not get one.B'caz that format is different.They wrote as

    The format of this information is probably different from that of the information stored with a block of storage obtained through scalar new.

    and also menctioned that

    It's also possible that scalar and array allocation employ different memory pools.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Yes but when you new and new[10] you can get the memory where the buffer is allocated and that can be watched what ever be the pool be.
     
  9. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Hi shabbir,
    Yes, we can get the memory where the buffer is allocated.But, with that we are not sure that the compiler is searching for that amount of memory only.When we allocate memory using new it will return the address if it is sucessful else null.But with this we don't know actually how much it is allocating.
    In case of char* a = "abcd";compiler is adding one more byte and it is placing '\0' . May be if we asked for allocating 10 bytes but internally it may take 12 bytes and in the last bytes it is storing the info. in different formatt.It is my guess.
    So if we check how much memory it is allocated manually? then it will be easy.otherwise Is it possible to allocate memory at a particular address? so that we menction the address and we try to allocate memory to that particular one.

    Regards,
    sharmila.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Thats what I have done and came to the conclusion that it does not store where ever it allocates the buffer.
    Yes using the placement new operator.
     
  11. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Hi shabbir,
    Can you tell me how to check whether memory is allocated or not manually? and also about placement new operator?

    Regards,
    sharmila
     
  12. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    Yes, link is right to say that.

    The allocation's size is kept somewhere. new [] probably stashes it just before or after the array. However, it's up to the implementation, so it can do whatever the hell it wants
     
  13. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    On failure new throws an exception bad_alloc() according to standard. Though some compiler doesn't conform to this and return NULL.
     
    Last edited: Jun 20, 2006
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    By watching the memory in the memory watcher available with VC++ 6
    Here is an example
    Code:
     #include <new>        // Must #include this to use "placement new"
     #include "Fred.h"     // Declaration of class Fred
     
     void someCode()
     {
       char memory[sizeof(Fred)];     // Line #1
       void* place = memory;          // Line #2
     
       Fred* f = new(place) Fred();   // Line #3 (see "DANGER" below)
       // The pointers f and place will be equal
     
       ...
     } 
    Line #1 creates an array of sizeof(Fred) bytes of memory, which is big enough to hold a object.
    Line #2 creates a pointer place that points to the first byte of this memory.
    Line #3 essentially just calls the constructor Fred::Fred(). The this pointer in the Fred constructor will be equal to place. The returned pointer f will therefore be equal to place.
     
  15. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Thank you Aztec for responding.
    Thank you Shabbir for placement new operator.Till now I did not know that we r having placement new operator also.
    Thanks once again.

    Regards,
    sharmila.
     

Share This Page