Using delete instead of delete[] for 1 integer

Discussion in 'C++' started by Money, Dec 24, 2006.

  1. Money

    Money Guest

    If I allocate memory like this

    int *ptr = new int[1];

    Can I apply delete ptr; instead of delete[] ptr; since I am only
    allocating memory for 1 integer.
    Money, Dec 24, 2006
    #1
    1. Advertising

  2. Money

    Jim Langston Guest

    "Money" <> wrote in message
    news:...
    > If I allocate memory like this
    >
    > int *ptr = new int[1];
    >
    > Can I apply delete ptr; instead of delete[] ptr; since I am only
    > allocating memory for 1 integer.


    Undefined. It may work, it may not. It all depends on how the compiler/OS
    is handling new/new[] and delete/delete[]. It may even appear to work and
    have side effects you're not aware of. In other words, I wouldn't do it.
    Jim Langston, Dec 24, 2006
    #2
    1. Advertising

  3. Money

    Money Guest

    Jim Langston wrote:
    > "Money" <> wrote in message
    > news:...
    > > If I allocate memory like this
    > >
    > > int *ptr = new int[1];
    > >
    > > Can I apply delete ptr; instead of delete[] ptr; since I am only
    > > allocating memory for 1 integer.

    >
    > Undefined. It may work, it may not. It all depends on how the compiler/OS
    > is handling new/new[] and delete/delete[]. It may even appear to work and
    > have side effects you're not aware of. In other words, I wouldn't do it.


    But why would it be undefined?
    delete ptr; will release memory for atleast 1 integer and that's what I
    want.
    Money, Dec 24, 2006
    #3
  4. Money

    Greg Guest

    Money wrote:
    > Jim Langston wrote:
    > > "Money" <> wrote in message
    > > news:...
    > > > If I allocate memory like this
    > > >
    > > > int *ptr = new int[1];
    > > >
    > > > Can I apply delete ptr; instead of delete[] ptr; since I am only
    > > > allocating memory for 1 integer.

    > >
    > > Undefined. It may work, it may not. It all depends on how the compiler/OS
    > > is handling new/new[] and delete/delete[]. It may even appear to work and
    > > have side effects you're not aware of. In other words, I wouldn't do it.

    >
    > But why would it be undefined?
    > delete ptr; will release memory for atleast 1 integer and that's what I
    > want.


    One reason is that arrays may be allocated differently than objects.
    For example, the runtime may store information about an allocation
    before the start of the block - and conceivably the format of this
    information could differ for an array allocation than an allocation of
    a non-array, single object. (Note that the size of the array makes no
    difference to the format selected, all arrays of any size are treated
    alike).

    So if the form of (delete or delete[]) when deleting an array or object
    does not match the form of the new operator (new or new[]) that was
    used to allocate it, then runtime would interpret the block's stored
    information incorrectly - by assuming the data is stored in a format
    that is not the same as the format it was stored in - and whatever
    happens after the point is uncertain (that is, undefined), and is
    unlikely to be good.

    Greg
    Greg, Dec 24, 2006
    #4
  5. Money

    Bo Persson Guest

    Money wrote:
    > Jim Langston wrote:
    >> "Money" <> wrote in message
    >> news:...
    >>> If I allocate memory like this
    >>>
    >>> int *ptr = new int[1];
    >>>
    >>> Can I apply delete ptr; instead of delete[] ptr; since I am only
    >>> allocating memory for 1 integer.

    >>
    >> Undefined. It may work, it may not. It all depends on how the
    >> compiler/OS is handling new/new[] and delete/delete[]. It may
    >> even appear to work and have side effects you're not aware of. In
    >> other words, I wouldn't do it.

    >
    > But why would it be undefined?
    > delete ptr; will release memory for atleast 1 integer and that's
    > what I want.



    Because the standard says so. :)

    The compiler is allowed to make it NOT work if it feels like. Or some other
    compiler you try, or the next release.

    Just don't do it!


    Why would you use new to allocate a single int anyway?


    Bo Persson
    Bo Persson, Dec 24, 2006
    #5
  6. Money

    terminator Guest

    On Dec 24, 2:04 pm, "Money" <> wrote:
    > Jim Langston wrote:
    > > "Money" <> wrote in message
    > >news:...
    > > > If I allocate memory like this

    >
    > > > int *ptr = new int[1];

    >
    > > > Can I apply delete ptr; instead of delete[] ptr; since I am only
    > > > allocating memory for 1 integer.

    >
    > > Undefined. It may work, it may not. It all depends on how the compiler/OS
    > > is handling new/new[] and delete/delete[]. It may even appear to work and
    > > have side effects you're not aware of. In other words, I wouldn't do it.But why would it be undefined?

    > delete ptr; will release memory for atleast 1 integer and that's what I
    > want.

    C++,s new/new[] and delete/delete[] are overloadable operators they
    might be instructed to place arrays on different heap than single
    objects.So your program
    might simplly crash.You are programing with high risk.
    terminator, Dec 24, 2006
    #6
  7. Money

    stork Guest

    Money wrote:
    > If I allocate memory like this
    >
    > int *ptr = new int[1];
    >
    > Can I apply delete ptr; instead of delete[] ptr; since I am only
    > allocating memory for 1 integer.


    It may or may not work, depending on the implementation, and even the
    version. But, it would not be portable code. You should ALWAYS use
    delete [] to delete something that was allocated with new [].
    stork, Dec 24, 2006
    #7
  8. Money

    Glen Dayton Guest

    Money wrote:
    > If I allocate memory like this
    >
    > int *ptr = new int[1];
    >
    > Can I apply delete ptr; instead of delete[] ptr; since I am only
    > allocating memory for 1 integer.
    >


    For Plain Old Datatype (POD) like an int, it likely not to
    matter. delete[] also destructs each of the elements of the
    array. If you use 'delete', you'll bypass destructing the
    elements of the array.

    Also, naked pointers are obscene. Never declare a unwrapped int
    *. Wrap it with a boost:scoped_array<> or boost:shared_array<>.
    This way you won't need to worry about whether to call
    delete[] or delete. Make the object take care of itself.

    /Glen Dayton
    Glen Dayton, Dec 24, 2006
    #8
  9. Money

    Salt_Peter Guest

    Money wrote:
    > If I allocate memory like this
    >
    > int *ptr = new int[1];
    >
    > Can I apply delete ptr; instead of delete[] ptr; since I am only
    > allocating memory for 1 integer.


    You can't because its not guaranteed. Although on most compilers the
    above might work you still are left with the uncertainty that it may
    fail. That is, an implementation of a primitive array is not required
    to make the above delete correctly (what the specific details of such
    an implementation might be doesn't matter). You've got other things to
    worry about.

    Whats relevent to you is the fact that if one day somebody decides that
    they need new int[2] instead of new int[1], your deletion should still
    work. And it will if you follow the standard, delete[] ptr is
    guarenteed.
    Salt_Peter, Dec 24, 2006
    #9
  10. Money

    Andre Kostur Guest

    Glen Dayton <> wrote in news:FnAjh.44$ji1.9
    @newssvr12.news.prodigy.net:

    > Money wrote:
    >> If I allocate memory like this
    >>
    >> int *ptr = new int[1];
    >>
    >> Can I apply delete ptr; instead of delete[] ptr; since I am only
    >> allocating memory for 1 integer.
    >>

    >
    > For Plain Old Datatype (POD) like an int, it likely not to
    > matter. delete[] also destructs each of the elements of the
    > array. If you use 'delete', you'll bypass destructing the
    > elements of the array.


    Not necessarily true. An array may be allocated differently than a
    single object (and we're not talking about construction/destruction).

    I could see one potential implementation as follows:

    - When allocating memory for a single object, allocate sizeof(int) +
    sizeof(object). Write the size of the object in the first sizeof(int)
    bytes, invoke the constructor on the sizeof(object) bytes, return a
    pointer to the object. (Perhaps there's customized allocators for
    different-sized objects)

    - When allocating memory for an array of objects, allocate sizeof(int) +
    sizeof(int) + arraysize * sizeof(object). Write the arraysize in the
    first sizeof(int) bytes, write sizeof(object) in the second sizeof(int)
    bytes, invoke the constructor on each of the sizeof(object) bytes.
    Return a pointer to the first object.

    - When using delete, invoke the destructor on the sizeof(object) bytes
    starting at the pointer, back up sizeof(int) bytes, hand this pointer
    back to wherever the memory came from (perhaps some global
    allocator/deallocator)

    - When using delete[], back up sizeof(int) + sizeof(int). Copy out the
    first sizeof(int) as the number of objects, copy the 2nd sizeof(int) for
    the object size. Loop over each of the sizeof(object) bytes (starting
    from the _end_ of the array) and invoke the destructor on each object in
    turn. Finally hand the pointer - sizeof(int) - sizeof(int) back to
    wherever the memory came from.


    As a result, if you allocate with:

    int * p = new int[1];

    You get 12 bytes allocated, starting at p - 8. (Let's assume no padding
    and sizeof(int) == 4)

    When you call:

    delete p;

    You end up attempting to hand p - 4 back to the OS. Since it didn't
    allocate that pointer, who knows what it's going to do. And in the
    theoretical case where different-sized allocations come from different
    pools, this memory block would be handed back to the wrong pool too (say,
    the 8-byte allocator instead of the 12-byte allocator).
    Andre Kostur, Dec 24, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    553
  2. rickman
    Replies:
    14
    Views:
    739
    rickman
    May 15, 2008
  3. gopesh patel
    Replies:
    5
    Views:
    2,096
    Stuart Golodetz
    Sep 16, 2010
  4. paul
    Replies:
    10
    Views:
    196
    Tom Werner
    Nov 10, 2006
  5. Alex Vinokur
    Replies:
    3
    Views:
    493
    Zoltan Juhasz
    Jun 15, 2012
Loading...

Share This Page