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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:47:23+00:00 2026-05-12T07:47:23+00:00

I am wondering about the delete[] operator in C++. (I am using Visual Studio

  • 0

I am wondering about the delete[] operator in C++. (I am using Visual Studio 2005).

I have an unmanaged DLL that is being called by a managed DLL. When I close this program after performing a few tasks while debugging, I am getting many (thousands?) of memory leaks, mostly 24 bytes – 44 bytes in size.. I suspect it might be due to a certain unmanaged DLL I have.

Anyway, from what I understand, if I have the following code:

char* pointer = new char[500]
/* some operations... */
delete[] pointer;

Then all the memory for it is freed up correctly, am I right?

What happens when I have the following code:

char* pointer = new char[500];
char* pointerIt = pointer;
/* some code perhaps to iterate over the whole memory block, like so */
for (int i = 0; i < 250; i++){ // only iterate halfway
    *pointerIt = 0;
    pointerIt++;
}

delete[] pointer;

The memory pointed to by pointer is deleted right? So it means that pointerIt is now not pointing to valid memory.. But that’s ok because I can set both pointers to NULL, right?

Anyway, what happens now if I do this:

char* pointerFirstPosition = new char[500];
char* pointerIt = pointerFirstPosition;

for (int i = 0; i < 250; i++){ // only iterate halfway
    *pointerIt = 0;
    pointerIt++;
}

delete[] pointerIt; // delete the pointer iterator...

Will this code delete the memory block pointed to by pointerIt up to pointerIt +500? or will it delete the memory block pointed to by pointerFirstPos to pointerFirstPos +500?

Could this result in a memory leak?

Sorry for the long winded message, I’m trying to get my message across clearly.

Thanks,

kreb

  • 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-12T07:47:23+00:00Added an answer on May 12, 2026 at 7:47 am

    First question set:

    char* pointer = new char[500] 
    /* some operations... */ 
    delete[] pointer;
    

    Then all the memory for it is freed up
    correctly, am I right?

    right.

    Second question set:

    char* pointer = new char[500];
    char* pointerIt = pointer;
    /* some code perhaps to iterate over the whole memory block, like so */
    for (int i = 0; i < 250; i++){ // only iterate halfway
        *pointerIt = 0;
        pointerIt++;
    }
    
    delete[] pointer;
    

    The memory pointed to by pointer is
    deleted right? So it means that
    pointerIt is now not pointing to valid
    memory.. But that’s ok because I can
    set both pointers to NULL, right?

    The memory pointer holds an address to is fully delted yes. Both pointer and pointerIt hold an address to invalid memory. Each pointer is simply a variable, and every variable is independent. So both store their own address independent of each other. The dereference operator * will simply give you the variable at that address. The variable at that address is a different variable than the pointer variable.

    Third question set:

    You should be deleting only the address that was allocated, the whole array. You’ll have undefined results if you try to delete a partial array. Could it result in a memory leak? Possibly, could it result in a crash? Possibly, could it result in ….? Possibly.

    So only delete what you allocate. If you allocate an array then you delete with delete[] if you delete a type that is not an array you delete with delete.

    Here is an eample of something that is fine just for clarity:

    char* pointer = new char[500];
    char* pointerIt = pointer;
    //This is fine because you are deleting the same address:
    delete[] pointerIt;
    //The memory that both variables point to is freed, do not try to free again
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the nowarn element instead. For example: <csc output="${build.dir.legacy}Library.dll" target="library"… May 13, 2026 at 9:06 pm
  • Editorial Team
    Editorial Team added an answer You want to use a WindowAdapter instead of a WindowListener.… May 13, 2026 at 9:06 pm
  • Editorial Team
    Editorial Team added an answer http://turbomanage.wordpress.com http://borglin.net/gwt-project May 13, 2026 at 9:06 pm

Related Questions

I've been thinking about the deployment process I am currently using, and I am
I have written a script that goes through a bunch of files and snips
We have a C++ library that we provide to several different clients. Recently we
I have four DB tables in an Oracle database that need to be rewritten/refreshed

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.