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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:06:58+00:00 2026-05-27T01:06:58+00:00

I have some code for you guru’s to check. I know this is probably

  • 0

I have some code for you guru’s to check. I know this is probably ( definitely ) not the best way to go about deleting memory, but just let me know if it would potential be OK.

I know I should be using smart pointers, but I will do this in my next revision of the code.

Anyway here is my problem.

I have a class that is initiated each time in a loop. I pass this class pointer to a vectors of pointers to an object which also change in each iteration of the loop. e.x.

vector<A*>* ex_A

I then use these pointers to vectors in the class and Map them to each other by using

map< int, vector<B*>* >*  ex_map.

This new map can then be accessed through the class by one of the public member functions.

At the end of Loop I wish to delete the memory so that the next new Class can be instantiated without seg faulting.

Here is some example code of what I am doing:

Inside Class MainClass.cxx

MainClass::MainClass(vector<A*>* ex_A, vector<B*>* ex_B, vector<C*>* ex_C): m_ex_A(ex_A), m_ex_B(ex_B), m_ex_C(ex_C) {}

MainClass::MakeMap(){

 vector<A*>::iterator itr_A = m_ex_A->begin();
 for(; itr_A != m_ex_A->end(); itr_A++){

    //This member function returns a vector of B* that are dependent of A
    vector<B*>* dependent_B = changeVectorB((*itr_A));

    /Defined in Header File
    //map< int, vector<B*>* >* m_mapAofB;
    //Now get Map index of A with vector of B*
    (*m_mapAofB)[(*itr_A)->Inedx()] = dependent_B;

   }

}

I will want to delete m_mapAofB at some point so I would loop through each value of the map (which is a pointer to vector of pointers). I use something similar to this URL to achieve this.

C++ Generic code for deleting pointer value in Map and vector of pointers

My main concern is when to delete this memory, as MainClass will be in a for loop. Should I delete the memory at the end of each iteration of the for loop. This would allow for the new instantiation of the MainClass??

e.x.

for(int i = 0; i < 100000; i++){
     //This will also change each loop
     Vector<A*> ex1;
     Vector<B*> ex2;         
     Vector<C*> ex3;

     MainClass* ex_main = new MainClass(ex1,ex2,ex3);


     ///Do something....

     //Finished with class now delete pointers
     ex_main->DeleteMemory();

     delete ex_main;
}
  • 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-27T01:06:59+00:00Added an answer on May 27, 2026 at 1:06 am

    I’m confused on many levels by this and think it needs a redesign – but all that aside:

    You state:

    as MainClass will be in a for loop. Should I delete the memory at the
    end of each iteration of the for loop. This would allow for the new
    instantiation of the MainClass??

    So, why do you believe you need to dynamically allocate MainClass in the first place if you’re planning on deleting the memory related to it at the end of each iteration? It can simply be a stack variable which is created at the top of the loop and destroyed each time it loops (or better yet, as described in Slade’s comment, you can declare it above your loop so it isn’t recreated every time – that would require you to have a public function to change it to mimic its constructor though). Secondly, wrap your pointers in objects (I don’t care if they’re smart pointers for real, just wrap them) so when your vectors go out of scope your wrappers destructors will automatically delete the dynamically allocated contents.

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

Sidebar

Related Questions

I have some code like this in a winforms app I was writing to
I have some code like this: If key.Equals(search, StringComparison.OrdinalIgnoreCase) Then DoSomething() End If I
I have some code where the timer EventHandler has this void timer_Tick(object sender, EventArgs
I have some code like this: JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.RegisterConverters(new JavaScriptConverter[] {
I have some code that looks like this (I left out the parts that
I have some code that retrieves an image from the web using this method
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some code which collects points (consed integers) from a loop which looks
I have some code in a javascript file that needs to send queries back
I have some code that gives a user id to a utility that then

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.