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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:34:16+00:00 2026-06-02T02:34:16+00:00

Would the following properly destroy everything (all appropriate destructors called and all memory freed)?

  • 0

Would the following properly destroy everything (all appropriate destructors called and all memory freed)?

Class* var[50];
var[0] = new SubClass();

delete[] *var;

Similar to

Class** var = new Class*[50];
var[0] = new SubClass();

delete[] var;

Or should I be iterating over the array and delete each individual object (which is what I thought delete[] did).

  • 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-02T02:34:18+00:00Added an answer on June 2, 2026 at 2:34 am
    Class* var[50];
    var[0] = new SubClass();
    
    delete[] *var;
    

    This is undefined behavior. The proper way is

    delete var[0]; //equivalent to delete *var;
    

    If you allocate memory for all 50 elements of the array, then you have to iterate and delete each one of them. Think of it this way:

    • for every new, you should have an associated delete
    • for every new[], you should have an associate delete[]
    • don’t mix up new with delete[] and new[] with delete as it leads to undefined behavior

    Your second snippet is illegal C++.

    EDIT As you’re clearly a beginner, let’s break this down a bit. Since the second snippet doesn’t even compile, I’m going to focus on the first one:

    Class* var[50];
    

    This declares an array of 50 pointers to Class. The pointers are dangling – they are not initialized and Class objects aren’t created.

    var[0] = new SubClass();
    

    This allocates memory for a SubClass object and assigns the first pointer in the array. All other pointers remain uninitialized.

    *var;
    

    returns the first element, which is a Class*. It’s equivalent to var[0].

    delete[] *var;
    

    attempts to call delete[] on a Class*, which was allocated with new, so it results in undefined behavior.

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

Sidebar

Related Questions

As the question states, would the following array require 5 bits of memory? $flags
How would I properly write the following INSERT using LIKE? provider = provider cursor.execute(INSERT
I would like to write the following code: boolean found = false; search(new SearchCallback()
Ho would I properly encode the following: # # -*- coding: utf-8 -*- >>>
In the following markup, "image 3" would the only visible div if all .image
Using mod_rewrite, what RewriteRule(s) would make the following two examples function properly? Example #1
Would following the table below be the best way of determining the access type
Why would the following swap action fail at random times? int i,p,a[11] = {0,1,2,3,4,5,6,7,8,9,10
I thought the following would be a pretty common task and assumed there would
I was hoping the following would be parseable in StAX, <something a=b/> <something a=b/>

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.