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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:55:03+00:00 2026-05-11T10:55:03+00:00

Alright, I think we all agree that what happens with the following code is

  • 0

Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed,

void deleteForMe(int* pointer) {      delete[] pointer; } 

The pointer could be all sorts of different things, and so performing an unconditional delete[] on it is undefined. However, let’s assume that we are indeed passing an array pointer,

int main() {      int* arr = new int[5];      deleteForMe(arr);      return 0; } 

My question is, in this case where the pointer is an array, who is it that knows this? I mean, from the language/compiler’s point of view, it has no idea whether or not arr is an array pointer versus a pointer to a single int. Heck, it doesn’t even know whether arr was dynamically created. Yet, if I do the following instead,

int main() {      int* num = new int(1);      deleteForMe(num);      return 0; } 

The OS is smart enough to only delete one int and not go on some type of ‘killing spree’ by deleting the rest of the memory beyond that point (contrast that with strlen and a non-\0-terminated string — it will keep going until it hits 0).

So whose job is it to remember these things? Does the OS keep some type of record in the background? (I mean, I realise that I started this post by saying that what happens is undefined, but the fact is, the ‘killing spree’ scenario doesn’t happen, so therefore in the practical world someone is remembering.)

  • 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. 2026-05-11T10:55:04+00:00Added an answer on May 11, 2026 at 10:55 am

    The compiler doesn’t know it’s an array, it’s trusting the programmer. Deleting a pointer to a single int with delete [] would result in undefined behavior. Your second main() example is unsafe, even if it doesn’t immediately crash.

    The compiler does have to keep track of how many objects need to be deleted somehow. It may do this by over-allocating enough to store the array size. For more details, see the C++ Super FAQ.

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

Sidebar

Related Questions

Alright, I have some data that I need to assign an int type identifier
Do you think it is alright from architectural stand-point to have ViewModel - View
Alright, so all modals already have an image at the top right to close
Alright, so I'm running into an issue with my code. What I have done
Alright so I just tried to cut down on lines of code by changing
Alright, I'm not the best with JOIN's and after all these years of working
Alright, so I've been trying to implement a simple binary search tree that uses
Alright, I'm confused as hell. I have an object that I store in a
Alright, so I think I'm pretty close to having what I need, but I'm
Alright, I am going to state up front that this question may be too

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.