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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:20:28+00:00 2026-06-17T13:20:28+00:00

OK, I suppose this question may sound too silly, but memory-management (especially in C/C++)

  • 0

OK, I suppose this question may sound too silly, but memory-management (especially in C/C++) has never been my strong point and since it’s usually not a noticeable thing, I tend to overlook it. So, forgive me if all this sounds stupid. However, since my current project involves A LOT of data and processing, memory consumption easily goes above 2GB in a matter of seconds and it definitely slows the whole thing down, so… it’s time I started thinking of ways how to fix it.


So, here is my situation…

My principal class (of which I’m creating some millions of instances (which hopefully are automatically deleted, since they stop being used), so supposedly this is the culprit) is (roughly) this :

class MyClass
{
    public:
        // My Constructors
        MyClass ();
        MyClass (std::string param);

        // My Destructor (still empty)
        virtual ~MyClass ();

        // Some methods
        void methodA(std::string moves);
        void methodB();

        //----------------------
        // My Variables
        //----------------------
        boost::array<int,64> arrA;
        boost::array<unsigned int,13> arrB;
        unsigned int key;

        boost::array<int,3> arrC;       
        int argA;
        int argB;
};

And this is (roughly – actual code has been altered) how the instances of the above class are created :

vector<MyClass*> SomeOtherClass::getListOfObjects()
{   
    vector<MyClass*> objects;

    for (int i=0; i<MAX_OBJS; i++)
    {
          // Do some preparatory work
          objects += new MyClass();
    }

    return objects;
}

And here’s how the results of the above function are being used :

void SomeOtherClass::doSth()
{
    vector<MyClass*> objs = this->getListOfObjects();
    int objsSize = objs.size();

    for (int i=0; i<objsSize; i++)
    {
        MyClass* obj = objs[i];

        // Do sth with obj

        delete objs[i];
    }
}

So, my question is :

  • What should I do in my destructor, so that when the object is not needed anymore and thus released, all of its “subcomponents” are released too? (e.g. the 2 boost::arrays)

  • Is there anything wrong you notice with the approach above?


Please let me know if there’s anything else you need to know about my implementation…

  • 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-17T13:20:29+00:00Added an answer on June 17, 2026 at 1:20 pm

    Your class members do not look to be dynamically allocated, in which case you don’t need to explicitly delete anything in the destructor. If you happened to leave out some pointers to allocated memory, in this question, which you allocate with new, you would need to delete these in the destructor.

    Remember, if you new you need to delete, similarly with new[]–delete[]. Barring the case of allocation to a std::unique_ptr.

    If your MyClass objects themselves are allocated on the heap with new, then you would have to delete them.

    p.s. if you are using C++11, you should probably use std::array now.


    From your new code, it is obvious that whoever keeps the list returned from getListOfObjects(), needs to call delete on each element when its to be destroyed. It is likely the destructor of SomeOtherClass.

    Alternatively you can wrap the MyClass* pointers within a std::unique_ptr or std::shared_ptr (or any of the boost smart pointers which might be relevant here) which would then delete automatically when the vector holding them goes out of scope and is destroyed.


    If doSth is accurate in its representation, and ensures that ALL instances of MyClass get deleted, then this code seems fine, from a memory leak standpoint.

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

Sidebar

Related Questions

I realize that this may sound like a silly question, but the last time
This may sound a newbie question, however I'm new to iOS dev. Suppose with
This may not sound like a coherient question, but I need to find some
This may be a silly question but i'm totally new to Apps Engine and
This may not be the typical stackoverflow question. A colleague of mine has been
You will suppose this is a big question.but there might be a simple problem
This question has been answered in multiple times. People end saying that the only
This may sound a newbie question, however I'm new to iOS. Customer wants to
Depending on your interpretation this may or may not be a rhetorical question, but
I'm working in Windows Phone 7, but I suppose the question may be Silverlight

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.