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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:45:38+00:00 2026-06-16T00:45:38+00:00

I have an array of pointers (that I created by calling new ptr*[size] ).

  • 0

I have an array of pointers (that I created by calling new ptr*[size]). All of these pointers point to an object that was also put on the heap.

What is the proper way to delete the array and all new’d ptr’s?

This is what I do now:

for (int i = 0; i < size; i++) delete array[i];
delete[] array; // Not sure since this double deletes array[0]

Does this do what I think it should?

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-16T00:45:39+00:00Added an answer on June 16, 2026 at 12:45 am

    Every pointer allocated with new gets a corresponding delete. Every pointer allocated with new [] gets a corresponding delete []. That’s really all you need to know. Of course, when you have a dynamically allocated array which contains dynamically allocated pointers the deallocation must occur in reverse order.

    So it follows that the correct idiom would be…

    int main()
    {
        int **container = new int*[n];
        for(int i = 0; i < n; ++i)
            container[i] = new int[size];
    
        // ... and to deallocate...
        for(int i = 0; i < n; ++i)
            delete [] container[i];
    
        delete [] container;
    }
    

    And then of course I say “stop doing that” and recommend you use a std::array or std::vector (and the template type would be unique_ptr<int>).

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

Sidebar

Related Questions

I have an array of pointers to a base class, so that I can
I have an array of pointers to Objective-C objects. These objects have a sort
I have an array of float values that is created in regular Python, that
Recently started working with pointers and have created a little script that is supposed
I have created an array of pointers. I have used some of them as
I am trying to create an array of pointers. These pointers will point to
I have a 3x3 array that I'm trying to create a pointer to and
What if i want to have an array of pointers to a function and
I couldn't find anything similar to this anywhere. I have an array of pointers
I have an array of double pointers, but every time I try do print

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.