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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:02:07+00:00 2026-05-26T03:02:07+00:00

Suppose I have a C++ class with two private variables. A fixed size array,

  • 0

Suppose I have a C++ class with two private variables. A fixed size array, data, and a pointer to that array, pnt.

class MyClass
{
   private:
      double *pnt;
      double data[2];
   public:
      myClass();
      virtual ~MyClass();
      double* getPnt() const;
      void setPnt(double* input);
};

MyClass::MyClass()
{

   double * input;
   data[0] = 1;
   data[1] = 2;

   input= data;
   setPnt(input);
}

MyClass::~MyClass()
{
 delete this->pnt; // This throws a runtime error
}


void MyClass::setPnt(double * input)
{
   pnt = input;
}

double * MyClass::getPnt() const;
{
   return pnt;
}

int main()
{
   MyClass spam; // Construct object
   delete spam; // Error C2440: 'delete' cannot convert from 'MyClass' to 'void*'
}

There are two problems with this code. First if I try to call delete on the object, I get:

Error C2440: ‘delete’ cannot convert from ‘MyClass’ to ‘void*’

Secondly, if I comment out the delete statement, I get a realtime error stating a debug assertion failed! and this:

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

My question is then: For a class with a pointer to a private fixed size array, how do I properly free memory, write/call the destructor?

P.S I can’t use vector or nice containers like that (hence this question).

  • 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-26T03:02:08+00:00Added an answer on May 26, 2026 at 3:02 am

    I see no static array. I see a fixed-size array. Also memory for data is allocated as part of the object.

    You must not explicitely delete a member of the class: the delete operator will take care of that IFF the instance was dynamically allocated.

     {
          MyClass x; // auto variable
     } // x destructor run, no delete operator
    

    vs.

     {
          MyClass* x = new MyClass(); // heap allocation variable
          delete x; // x destructor run, ::delete de-allocates from heap
     } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose you have two (or more) classes with private member vectors: class A {
Suppose I have a class public class MyClass { private Set<String> set = new
Suppose I have two instances of the same class. The class has a pointer
Suppose that I have two classes, first a class without any properties, fields or
Suppose I have the following C# class class MyClass { private int _i; private
I suppose that this is an interesting code example. We have a class --
Suppose we have following two classes: class Temp{ public: char a; char b; };
Suppose I have two classes defined the following way: public class A { public
Suppose I have two classed Base and Derived , i.e.: #include <iostream> class Base
Suppose I have the following class: public class Foo { private List<Integer> list =

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.