🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

array getting filled with trash values

Started by
44 comments, last by NikiTo 4 years, 4 months ago

I initiate an array in a function and check the values within that array right after initialisation and everything is fine, down the road after some code is executed, I check the values again they are modified even though I haven`t touched the array (no direct edit of values). What`s causing this? do I need to place the initialisation outside the function so that the array doesn`t get affected by whatever goes wrong in the said function?

My project`s facebook page is “DreamLand Page”

Advertisement

You probably have a ‘write out of bounds bug’ somewhere else, causing memory corruption, and just by luck the corruption ends up affecting this certain array.

This is hard to track down. You may comment out sections of code that my be buggy, which changes how things are compiled and so also can change which memory the bug affects.
It may be no longer this certain array, but some other unknown memory. So it may look the bug has gone away but it is still there.

Thanks, I thought the code executed afterwards somehow spilled over.

My project`s facebook page is “DreamLand Page”

… good practice is to add your array to the ‘watch’ tab of your debugger, then step through the program and see when the values change.

You should do this immideatly as long the bug shows up, which is good opportunity to find it.

@JoeJ How can a debugger watch a whole array? Does it compare very memory access to the address range? I know, I am late to the game, but don't we have languages / libraries to detect array out of bounds exceptions. You know: premature optimization and all that.

arnero said:
Does it compare very memory access to the address range?

Yes. In Visual Studio the values that change in a step even show up in red (or bold… something visible for sure).

arnero said:
but don't we have languages / libraries to detect array out of bounds exceptions.

With C++ you get ‘free’ range checks when using std::array instead plain array. It's free because checks are done only in debug mode.

In release performance is the same as using plain native array. (But i never checked myself if this really holds true)

JoeJ thanks for advices

My project`s facebook page is “DreamLand Page”

while trying to track down the bug I`ve placed two check points one right after initialisation and another one after a tiny chunk of code but there is almost nothing going on between the two check points. Could the error rely somewhere else?

struct VERTEXGROUP
{
	VERTICE Vertici[100];
	LINIE Linii[100];
	int ArraySizeOfLines;
	int GroupVertexIDCounter;
	int LineCount;
	int x;
	int y;
	int z;
};

VOID AddGroup(int ID)
{
	VertexGroups[ID].LineCount = 0;
	VertexGroups[ID].GroupVertexIDCounter = 0;
	VertexGroupsCount = VertexGroupsCount + 1;
}

VOID AddvertexR(int x, int y, int z, int Group)
{
VertexGroups[Group].Vertici[VertexGroups[Group].GroupVertexIDCounter].x = x;
VertexGroups[Group].Vertici[VertexGroups[Group].GroupVertexIDCounter].y = y;
VertexGroups[Group].Vertici[VertexGroups[Group].GroupVertexIDCounter].z = z;
VertexGroups[Group].GroupVertexIDCounter++;
}


//check point one 
	AddGroup(0);
	AddvertexR( 20,0,100,0);
	AddvertexR( 120,0,100,0);
	AddvertexR( 120,0,200,0);
	AddvertexR( 20,0,200,0);
	AddvertexR( 70,80,150,0);
	
//checkpoint 2

if I comment out AddvertexR calls the bug dissapers.

My project`s facebook page is “DreamLand Page”

Where do you set GroupVertexIDCounter to zero?

Post code to AddGroup() too so we can see it all…

VOID AddGroup(int ID)
{
	VertexGroups[ID].LineCount = 0;
	VertexGroups[ID].GroupVertexIDCounter = 0;
	VertexGroupsCount = VertexGroupsCount + 1;
}

My project`s facebook page is “DreamLand Page”

This topic is closed to new replies.

Advertisement