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 5846937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:38:51+00:00 2026-05-22T12:38:51+00:00

It’s bothering me what delete [] actually does, so I just tried some code

  • 0

It’s bothering me what delete [] actually does, so I just tried some code and I was shocked with the results

Test #1:

int main()
{
    int *d;
    while(true)
    {
        d = new int[10];
        delete []d;
    }
}

The program doesn’t consume any memory at all, as expected.

Test #2:

int main()
{
    int *d;
    while(true)
    {
        d = new int[10];
        delete [](d + 5);
    }
}

Though in every loop there should be at least 20 bytes (for the five ints it reserves at the beginning of array) reserved which are not deleted this program also doesn’t consume any memory!

Test #3:

int main()
{
    int *d;
    while(true)
    {
        d = new int[10];
        delete []d;
        *d=1;
    }
}

This one caused access violation as expected (seems all the memory is deleted after delete []d).

Test #4:

int main()
{
    int *d;
    while(true)
    {
        d = new int[10];
        delete [](d+5);
        *d=1;
    }
}

This one was the most amazing, though the while doesn’t consume any memory the program doesn’t produce any access violations either, I’m just wondering where *d is storing it’s data?

(By the way all programs are compiled using no-optimization!)

Now the main question :

What if I allocated an array and I’ve done working with half of it, can’t I by any chance release that half and keep the other half?

  • 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-22T12:38:52+00:00Added an answer on May 22, 2026 at 12:38 pm

    it’s bothering me what delete []
    actually does

    You’re not supposed to worry about what delete[] actually does. It’s for all intents and purposes a black box with certain rules on how to use it properly. The only time you need to worry about what it needs to actually do is if you’re writing a compiler or a C++ runtime (e.g. operating systems, etc.)

    With respect to those “certain rules on how to use it properly”, Test #2 and #4 invokes undefined behavior:

    ISO C++ 2003 Standard 5.3.5 Delete [expr.delete]

    1 The delete-expression operator destroys a most derived object (1.8) or array created by a new-expression.

        delete-expression:
            ::opt delete cast-expression
            ::opt delete [ ] cast-expression
    

    The first alternative is for non-array
    objects, and the second is for arrays.
    The operand shall have a pointer type,
    or a class type having a single
    conversion function (12.3.2) to a
    pointer type. The result has type
    void.

    2 If the operand has a class type, the
    operand is converted to a pointer type
    by calling the above-mentioned
    conversion function, and the converted
    operand is used in place of the
    original operand for the remainder of
    this section. In either alternative,
    if the value of the operand of delete
    is the null pointer the operation has
    no effect. In the first alternative
    (delete object), the value of the
    operand of delete shall be a pointer
    to a non-array object or a pointer to
    a sub-object (1.8) representing a base
    class of such an object (clause 10).
    If not, the behavior is undefined.
    In the second alternative (delete array), the value of the operand of
    delete shall be the pointer value
    which resulted from a previous array
    new-expression. If not, the
    behavior is undefined.
    [Note: this
    means that the syntax of the
    delete-expression must match the type
    of the object allocated by new, not
    the syntax of the new-expression. ]

    “Undefined Behavior” means that anything can happen, including the behavior you just described.

    These expressions that you have in Tests #2 and #4 are in violation of 5.3.5/2 and will cause undefined behavior (Test #3 will also cause undefined behavior, but for a different reason).

    d = new int[10];
    delete [](d + 5);
    

    The delete[] line violates 5.3.5/2 because pointer value you pass to delete[] wasn’t the same value that was given to you from new int[].

    So if the new int[] command gives you 0xA01D2CE9 and you pass in 0xA01D2CE9 + 5 to delete[], you cannot possibly reason about or predict what will happen because you have broken the rules of the language. What will actually happen will be dependent on how the compiler and/or the operating system handles new[] and delete[]. That can range from nothing wrong happening to completely crashing your system, and everywhere in between.

    In other words, just don’t write things like delete [](d + 5);.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into

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.