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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:41:39+00:00 2026-06-04T11:41:39+00:00

I got really stuck trying to deallocate memory using delete without it being in

  • 0

I got really stuck trying to deallocate memory using delete without it being in a for loop.

    MyClass *Array[10];

cout << "Step 1 - Allocation" << endl << endl;
Array[0] = new MyClass();
Array[1] = new MyClass();
Array[2] = new MyClass(2, 4.6);
Array[3] = new MyClass(*Array[2]);
Array[4] = new MyClass();
Array[5] = new MyClass(13, 66.6);
Array[6] = new MyClass(75, 9.43);
Array[7] = new MyClass(*Array[6]);
Array[8] = new MyClass(*Array[1]);
Array[9] = new MyClass(*Array[3]);

cout << endl << "Step 2 - Write" << endl << endl;
for(int i=0; i<10; i++)
{
    Array[i]->write();
    cout << endl;
}

cout << endl << "Step 3 - Deallocation" << endl << endl;

I tried delete[] Array, but it doesn’t work.

The code must stay as it is as it’s correct by what is asked to do. The only thing is to add a delete (single-lined and not a for loop) to delete Array.

  • 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-04T11:41:40+00:00Added an answer on June 4, 2026 at 11:41 am

    You need to delete each object that you’ve created using new:

    for (unsigned i(0); i < 10; ++i)
        delete Array[i];
    

    Array itself is a local variable; you did not use new to create it, so you do not use delete to destroy it. It will be destroyed when it goes out of scope.

    To address the updated constraint that the deletion must be done “single-lined and not a for loop,” one could, I suppose, write:

    delete Array[0]; delete Array[1]; delete Array[2]; delete Array[3]; delete Array[4]; delete Array[5]; delete Array[6]; delete Array[7]; delete Array[8]; delete Array[9];
    

    This is a single line of code and uses no loops. It is not possible to use a single delete expression to destroy what are ten effectively unrelated objects.


    But do not do this. Really. Manual resource management is dangerous and very difficult to do correctly. C++ has automatic resource management capabilities and you should use them. In this case, it would be far better to use a std::vector<MyClass>:

    std::vector<MyClass> Array;
    Array.push_back(MyClass());
    Array.push_back(MyClass());
    Array.push_back(MyClass(2, 4.6));
    Array.push_back(MyClass(Array[2]));
    // Etc.
    

    There’s no new in this example, thus no delete is required. The std::vector will clean everything up when it is destroyed.

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

Sidebar

Related Questions

I am trying to implement the GJK-algorithm but I got stuck instantly. The problem
Really got stuck on this simple regex. Need it to validate a string, that
I'm really stuck on this one... Basically, I'm trying to make 2 pages always
We've got a problem when trying to generate and use a really large proxy
I got stuck. I am trying to form a function that will eat classless
I was hoping for some help on this as I'm really stuck after trying
We're being really stuck here so I decided to ask your help. Yesterday I've
I got stuck on trying to detect the pair of name and value of
I'm trying to redirect my response but I got stuck at the link path.
Guys i'm really embaraced to ask for this, but i got stuck and i

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.