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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:48:52+00:00 2026-06-14T20:48:52+00:00

#include <stdio.h> #include <iostream> #include <string> using namespace std; class myClass{ public: int *num1;

  • 0
#include <stdio.h>
#include <iostream>
#include <string>


using namespace std;

class myClass{

public:
    int *num1;
    myClass();


};

myClass::myClass(){
    num1 = new int[1];
    num1[0] = 10;
}


int main()
{
    myClass *myclass;

    myclass = new myClass[10];

    cout << myclass[0].num1[0] << endl;

    delete &myclass[0];
    cout << myclass[0].num1[0] << endl;

}

I want to delete the first instance of myclass (myclass[0]).

This code does not run correctly, it fails during the delete part. There is probably something I am missing.

What did I do wrong?

  • 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-14T20:48:53+00:00Added an answer on June 14, 2026 at 8:48 pm

    Create a function inside of your class the handles the deletion of its private members, maybe called FreeMem(int index)

    void myClass::FreeMem()
    {
        delete [] num1
    }
    

    But honestly, freeing memory of an object without the use of a destructor in this sort of a program is hazardous and downright bad practice. I would recommend freeing the memory in your destructor, so when the object terminates it frees the memory,

    myClass::~myClass()
    {
        delete [] num1;
    }
    

    Another thing to note on, if you’re only creating one value in your dynamic variable, it would be easier to write it as:

    int * pnum = new int;
    //or in your class..
    pnum = new int;
    

    among other things, you have a lot of flaws in your program. I would recommend re-reading up on classes again.

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

Sidebar

Related Questions

#include<stdio.h> #include<iostream> #include<fstream> #include<string.h> using namespace std; class base { public: int lookup(char c);
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int
here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
I have the following code: #include <iostream> #include <stdio.h> using namespace std; template <class
I have the following problem #include <iostream> #include <stdio.h> #include <string.h> int main (){
This works: #include <iostream> using namespace std; but this fails: #include <stdio> When is
#include <iostream> #include <string.h> #include <iomanip> #include <cstring> #include <cassert> using namespace std; #ifndef
I have the following: #include<iostream> #include<unordered_map> #include<tuple> using namespace std; class CTest { //
I have // file BoardInitializer.h #include <stdio.h> #include <tchar.h> #include <string> #include <iostream> using
I'm trying to generate new path using boost::filesystem as follows #include <iostream> #include <string>

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.