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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:12:04+00:00 2026-06-03T03:12:04+00:00

Suppose I have a class: class ClassX{ private: int* p; int i; …. }

  • 0

Suppose I have a class:

class ClassX{
   private:
      int* p;
      int i;
   ....
}

And somewhere I do:

ClassX x;
.....
//and in a friend function or in a ClassX function
p = (int*) malloc (.....);

Then when x exit its scope a the destructor is invoked; but it does not free the memory allocated by the malloc right?

And if I redefine it:

ClassX::~ClassX(){ delete [] p; }

it frees the memory allocated by the malloc but not the memory allocated for the class’ fields (i.e. i and p)?

Am I missing something?

Thank you.

  • 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-03T03:12:05+00:00Added an answer on June 3, 2026 at 3:12 am

    First of all, remember these things:

    1. Things allocated by malloc need to be deallocated by free
    2. Don’t use malloc
    3. Things allocated by new must be deallocated by delete
    4. Things allocated by new[] must be deallocated by delete[]
    5. One delete for every new and one delete[] for every new[]
    6. Don’t use any of the above

    Then, you are correct in thinking that, without a destructor, the memory allocated by malloc will not be freed. If you follow the rules above, then you need to use free (not delete[]) to deallocate it:

    ClassX::~ClassX() { free(p); }
    

    However, you shouldn’t be using malloc in C++ first of all since it doesn’t call the constructors of objects, you should use new:

    ClassX::ClassX() : p(new int) { }
    
    // NOW we use delete since we used new (not delete[] since we didn't use new[])
    ClassX::~ClassX() { delete p; }
    

    However, if you do that, you have to write a copy constructor, copy assignment operator, move constructor, and move assignment operator. So let’s look at an even better way:

    class ClassX{
    private:
        ClassX();
    
        std::unique_ptr<int> p;
        int i;
        ....
    };
    
    // we have to break rule #6 here
    ClassX::ClassX() : p(new int) { }
    

    Now you don’t even have to write a destructor, you can just let the smart pointer deal with it for you because it will automatically call delete on the thing you made with new when it’s destructor is called. Which leads us to…

    Your other question:

    it frees the memory allocated by the malloc but not the memory allocated for the class’ fields (i.e. i and p)?

    That’s about 1/4 correct. Since i and p are members of the class, they are automatically deallocated when the enclosing class is deallocated, and if they were classes themselves, their destructors would be called. So if you put delete in your destructor, that takes care of the memory allocated by new, and i and p are cleaned up automatically, and everything’s good. (You were only 1/4 correct because you used the wrong deallocation function.)

    This means that, if you use a smart pointer, the smart pointer’s destructor will be called when your object is destructed, and it will automatically deallocates what you allocated with new for you. So you don’t even need to worry about it.

    This is all assuming you really want to have a dynamic int as part of your class. If you can though, you’d much rather store the int by value (not store a pointer to it) and that way you don’t have to mess with smart pointers, deallocation, or any of that other stuff.

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

Sidebar

Related Questions

Suppose you have a simple class like this: class foo{ private: int* mData; int
Suppose you have this class: public class A { private int number; public setNumber(int
suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int
Suppose I have a class: class test { public: void print(); private: int x;
So suppose I have a class like this one: class Point { private: int
Suppose I have the following hierarchy: class A { public: A() private: int aa;
Suppose I have this class: class Int{ private: int x; public: Int(int i) :
Suppose I have a class like this class A { private int _x; //must
Suppose I have the following C# class class MyClass { private int _i; private
Suppose I have this code class Base{ public: int getVal(); private: int 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.