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

  • SEARCH
  • Home
  • 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 7936609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:16:01+00:00 2026-06-03T22:16:01+00:00

Possible Duplicate: How does delete[] “know” the size of the operand array? How does

  • 0

Possible Duplicate:
How does delete[] “know” the size of the operand array?
How does the delete in C++ know how many memory locations to delete

I know it’s a rather simple question but I a not sure about the difference (if any) between this lines :

double * a = new double[100];
delete[] a;
delete a;
free ((void*)a);

First off, would all of these calls (used each without the others) work the same way and free sizeof(double)*100 bytes?
Which lead me to the 2nd question, how does the program keep track of the size of the allocated memory? For instance if I send my a pointer to a function, then delete[] this pointer from within my function, would I also free the same amount of memory?

Thanks

  • 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-06-03T22:16:03+00:00Added an answer on June 3, 2026 at 10:16 pm

    The difference, oversimplified, is this:

    delete[] a;
    

    Is correct. All others are incorrect, and will exhibit Undefined Behavior.

    Now, in reality, on all the compilers I use daily, delete a; will do the right thing every time. But you should still not do it. Undefined Behavior is never correct.

    The free call will also probably do the right thing in the real world, but only because the thing you’re freeing doesn’t have a non-default destructor. If you tried to free something that was a class with a destructor, for example, it definitely wouldn’t work — the destructor would never be called.

    That’s one of the big differences (not the only difference) between new/delete and malloc/free — the former call the constructors and destructors, while the latter meerly allocate and dealocate space.

    Incorporating something @Rob said in his now-deleted post:

    The simple rule is this: every new[] requires exactly one delete[].
    Every new requires exactly one delete. malloc requires free. No
    mix-and-match is allowed.

    As to the question of how delete[] knows how many elements to delete, please see this response to a previous duplicate question.

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

Sidebar

Related Questions

Possible Duplicate: How does delete[] “know” the size of the operand array? Assume i
Possible Duplicate: How does delete[] “know” the size of the operand array? In the
Possible Duplicates: How does delete[] “know” the size of the operand array? ( POD
Possible Duplicate: Does std::vector.clear() do delete (free memory) on each element? I have a
Possible Duplicate: ( POD )freeing memory : is delete[] equal to delete ? Does
Possible Duplicate: What does “DateTime?” mean in C#? What does the ? mean after
Possible Duplicate: Why does GetErrorMessage return “wrong password”, when the user name is wrong?
Possible Duplicate: Does it make sense to use the <table> tag on a “modern”
Possible Duplicate: Does anyone know of a good C# API for Subversion? I'm designing
Possible Duplicate: Does anyone beside me just NOT get ASP.NET MVC? I dont know

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.