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

Recommended Answers

All 5 Replies

Firstly what is the actual error?

It's possible that you have (silently) written past the bounds of the array. Without seeing the rest of your code i'd say the likely issue is this: for (int i=0;i==SprTotal;i++) The array index of SprTotal (the number of sprites?) in the original array should be one past the end. So the last element in SprTmp is actually equal to a block of memory after the end of the SprArray.
Also potentially you have a heap corruption issue (is your app multi-threaded?).

Realistically - unless for the sake of interest - that's neither here nor there. That method is horribly inefficient and for your purposes a basic linked-list will be MUCH more efficient and a much cleaner approach.

Firstly what is the actual error?

It's possible that you have (silently) written past the bounds of the array. Without seeing the rest of your code i'd say the likely issue is this: for (int i=0;i==SprTotal;i++) The array index of SprTotal (the number of sprites?) in the original array should be one past the end. So the last element in SprTmp is actually equal to a block of memory after the end of the SprArray.
Also potentially you have a heap corruption issue (is your app multi-threaded?).

Realistically - unless for the sake of interest - that's neither here nor there. That method is horribly inefficient and for your purposes a basic linked-list will be MUCH more efficient and a much cleaner approach.

Heap corruption detected: after normal block #56, CRT detected that the application wrote to memory after the end of heap buffer. was the error, I think you might have a point about my for loop, could well be the issue (going to check it now). My App is not multi threaded, I learnt to program when I was only sweet sixteen, there was no multi threading I was aware of back then, so I don't know anything about implementing it.

My understanding of Linked lists is that they would not be very effective for accessing a particular item without running though the entire chain, How would you implement them in this context?

[/edit] Well I just tried using

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

and that failed, so then I tried removing the for loop altogether by commenting it out of the code. Same result, same error.
I also double checked the memory address given by the error and it is the same as that pointed to by sprArray, also the error is definitely generated by the delete line.

Heap corruption detected: after normal block #56, CRT detected that the application wrote to memory after the end of heap buffer. was the error, I think you might have a point about my for loop, could well be the issue (going to check it now). My App is not multi threaded

Based on the error and single-threadedness it looks very much like that is the issue then.

My understanding of Linked lists is that they would not be very effective for accessing a particular item without running though the entire chain, How would you implement them in this context?

Yes that is very true. For adding and removal it is much more efficient than the array copying but for accessing individual elements you would need to iterate over them. Alternatively Vectors are very easy (and type-safe ;P) so I would HIGHLY recommend looking into them, they would be more efficient also.
As for implementation of vectors in your situation, look at:
http://www.cplusplus.com/reference/stl/vector/at/
for an example. Obviously you would use vector<Sprite *> where they use ints. Template arguments <> are best read as 'of', where the above would be a vector of Sprite pointers.

Based on the error and single-threadedness it looks very much like that is the issue then.

Yes that is very true. For adding and removal it is much more efficient than the array copying but for accessing individual elements you would need to iterate over them. Alternatively Vectors are very easy (and type-safe ;P) so I would HIGHLY recommend looking into them, they would be more efficient also.
As for implementation of vectors in your situation, look at:
http://www.cplusplus.com/reference/stl/vector/at/
for an example. Obviously you would use vector<Sprite *> where they use ints. Template arguments <> are best read as 'of', where the above would be a vector of Sprite pointers.

Thank you very much, after an entire cup of espresso and talking about what you said to my co-developer I found the issue, I had made the foolish Basic style error of accessing the array by the number of elements. Thus trying to access element 1 when I had 1 element (and that is number 0) thus Array[1] can't work when only Array[0] exists! (so very silly of me!). Just what happens when your programming at 2AM I guess :P. However you pointed me in the right direction.

As for using linked lists I do need to access individual elements repeatedly and in random orders, thus I don't think that would work that well for me. However I will read up on using Vectors and perhaps update my engine in the near future. Thanks again ^_^.

No worries.

Yes you were copying a block of memory past the end of the array ;) That case of indexes versus num elements...that's not an uncommon one.
I suppose another nice thing about vectors is that if you do that you will recieve an error message at runtime that will tell you what you did wrong (and actual 'out of bounds' error) ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.