You are here:




I have this loop in my program where I repetedly create an array of pointer to class MyTree (declared elsewhere).

MyTree *array_of_pointers[rows];
//do something

My program workss but after lots of cycles I get memory overflow. This is because I don't empty the memory and the arrays of pointer accumulate. I don't know how to delete this array after each while cycle. How do you do this? If I use any other method of creating this array, my program doesn't work. Do I need to somehow declare this array dynamically? How? Thanks a lot!

Hi, Eric.

Without further context as to what happens in the loop and what exactly a MyTree class consists of, I'm not sure how much help I can be.  That said, I'll go over a couple of things here.

First, if you are creating everything within the loop, every time the loop restarts, it should all fall out of scope and be destroyed.  So, looping repeatedly should have no bearing on memory.  You will have to make sure that what you are declaring is not larger than your stack (or that you don't run out of stack space while executing commands during the loop), but if you can loop once, you should be able to loop indefinitely.

If anything is declaring dynamic memory within any of that (the MyTree class, for example), then you will need to make sure you free that memory.  It should be set up such that it will free when the object itself is destroyed (which happens when it falls out of scope), and if that isn't happening, then it needs to be fixed.

Finally, if you are declaring a large block of memory (or lots of memory, whether it's a single contiguous block or not), it is best to declare that in the heap rather than on the stack.  This means using new/delete to declare/destroy the items.

Just looking at the one line that does contain code in your example, I'd say you're declaring the array of pointers and dynamically allocating the MyTree objects within that array.  If that's the case, then you need to be sure that you go through and delete any allocated MyTree objects before you loop again.  This would look something like:

   while (condition)
       MyTree* array[rows] = { 0 };
       // do stuff start
       array[currentRow] = new MyTree;
       // do stuff end
       for (int i = 0; i < rows; ++i)
         if (array[i])
         delete array[i];

OK, if I've completely missed the mark or otherwise have been less than helpful, or if you just have further questions about anything I've written, please let me know.  I'm always happy to help.


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2017 All rights reserved.