Hello, my first post here, mostly due to every question I placed on google the answer is here (normally answered by a fellow Anime fan called Narue, someone's got their thinking cap on ^_^)
Anyhow the problem is as follows, I'm writing a 2D game engine using DirectX9 (not relivent but interesting) I've created a class to deal with sprites and am using that class via pointers, as I need to dynamically allocate new memory for the sprites depending on external content at runtime I'm creating space for them using New() and removing it afterwards with Delete(). That works fine, however to keep track of my near-endless sprites I need an array of pointers leading the the sprite class with numeric values and strings associated with each sprite.
I've coded a new class to deal with this, the problem lies in my function to add a new item to this class, as the maximum number isn't available the array's must be reallocated at runtime as new items are added (or removed).
All the other parts of my code appear to work fine, except for the reallocation code, I considered using vectors originally but am mostly unfamiliar with their use and if they could be applied to this particular code. Thus continue using New() and Delete() to witch I am more familiar.
During runtime the program spits out a written past end of heap error, and debugging points to the delete during reallocation (that seems wrong given it's freeing the memory not writing to it) Thus I lowered the number in my loop significantly, (i==SprTotal-5) (with ten entries at start), however the same error was produced when it seems impossible to me that I am running past the end of the array.
Thus I have come to the understanding that my use of memory allocation is incorrect in some way and turn to the many minds of the internet for help. I really would like to avoid where possible initialising all the items as globals as this would defeat the purpose of having a game 'engine' in the first place.
Sprite is class containing sprites (textures coords etc), and obviously the pointers used for new memory are global to the source.
constructor
SpriteArray::SpriteArray()
{
SprTotal=0; //total sprites at first load
SprArray=new Sprite*[SprTotal]; //allocate memory dynamically
NameArray=new LPCWSTR[SprTotal];
SprNum=new int[SprTotal];
SprArray[SprTotal]=0;
NameArray[SprTotal]=L"null";
SprNum[SprTotal]=SprTotal;
}
Section with error
int ItemNum = SprTotal+1; //Current Item Number
SprTotal++; //add one to Sprite array total
Sprite** SprTmp = new Sprite*[SprTotal]; //Allocate a temp array to handle them
for (int i=0;i==SprTotal;i++) //transfer every item to the new array
{
SprTmp[i]=SprArray[i];
}
SprTmp[SprTotal]=Spr; //and add the new entry
delete [] SprArray; //deallocate Sprite array
SprArray = SprTmp; //make SprArry point to new array