Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 509071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:58:21+00:00 2026-05-13T06:58:21+00:00

I know that when delete [] will cause destruction for all array elements and

  • 0

I know that when delete [] will cause destruction for all array elements and then releases the memory.

I initially thought that compiler wants it just to call destructor for all elements in the array, but I have also a counter – argument for that which is:

Heap memory allocator must know the size of bytes allocated and using sizeof(Type) its possible to find no of elements and to call appropriate no of destructors for an array to prevent resource leaks.

So my assumption is correct or not and please clear my doubt on it.

So I am not getting the usage of [] in delete [] ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:58:22+00:00Added an answer on May 13, 2026 at 6:58 am

    Scott Meyers says in his Effective C++ book: Item 5: Use the same form in corresponding uses of new and delete.

    The big question for delete is this: how many objects reside in the memory being deleted? The answer to that determines how many destructors must be called.

    Does the pointer being deleted point to a single object or to an array of objects? The only way for delete to know is for you to tell it. If you don’t use brackets in your use of delete, delete assumes a single object is pointed to.

    Also, the memory allocator might allocate more space that required to store your objects and in this case dividing the size of the memory block returned by the size of each object won’t work.

    Depending on the platform, the _msize (windows), malloc_usable_size (linux) or malloc_size (osx) functions will tell you the real length of the block that was allocated. This information can be exploited when designing growing containers.

    Another reason why it won’t work is that Foo* foo = new Foo[10] calls operator new[] to allocate the memory. Then delete [] foo; calls operator delete[] to deallocate the memory. As those operators can be overloaded, you have to adhere to the convention otherwise delete foo; calls operator delete which may have an incompatible implementation with operator delete []. It’s a matter of semantics, not just keeping track of the number of allocated object to later issue the right number of destructor calls.

    See also:

    [16.14] After p = new Fred[n], how does the compiler know there are n objects to be destructed during delete[] p?

    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. There are two popular techniques that do this. Both these techniques are in use by commercial-grade compilers, both have tradeoffs, and neither is perfect. These techniques are:

    • Over-allocate the array and put n just to the left of the first Fred object.
    • Use an associative array with p as the key and n as the value.

    EDIT: after having read @AndreyT comments, I dug into my copy of Stroustrup’s “The Design and Evolution of C++” and excerpted the following:

    How do we ensure that an array is correctly deleted? In particular, how do we ensure that the destructor is called for all elements of an array?

    …

    Plain delete isn’t required to handle both individual objects an arrays. This avoids complicating the common case of allocating and deallocating individual objects. It also avoids encumbering individual objects with information necessary for array deallocation.

    An intermediate version of delete[] required the programmer to specify the number of elements of the array.

    …

    That proved too error prone, so the burden of keeping track of the number of elements was placed on the implementation instead.

    As @Marcus mentioned, the rational may have been “you don’t pay for what you don’t use”.


    EDIT2:

    In “The C++ Programming Language, 3rd edition”, §10.4.7, Bjarne Stroustrup writes:

    Exactly how arrays and individual objects are allocated is implementation-dependent. Therefore, different implementations will react differently to incorrect uses of the delete and delete[] operators. In simple and uninteresting cases like the previous one, a compiler can detect the problem, but generally something nasty will happen at run time.

    The special destruction operator for arrays, delete[], isn’t logically necessary. However, suppose the implementation of the free store had been required to hold sufficient information for every object to tell if it was an individual or an array. The user could have been relieved of a burden, but that obligation would have imposed significant time and space overheads on some C++ implementations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 258k
  • Answers 258k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The two are (nearly) identical. When you define the initializer… May 13, 2026 at 11:02 am
  • Editorial Team
    Editorial Team added an answer I would use !sequence.Any(), personally. If you really need to,… May 13, 2026 at 11:02 am
  • Editorial Team
    Editorial Team added an answer select title from user This returns multiple rows, which won't… May 13, 2026 at 11:02 am

Related Questions

We have a class whose semantic behaviour is like the following :- struct Sample
I am looking into using Git on a massive scale. I was hoping to
While I do know the above goal is rather challenging to achieve, I know
I'm currently working on a TimerJob which does some site collections management. When the
In my specific case, I am making use of a discriminator column strategy. This

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.