FOR loop acting strange

Started by
3 comments, last by Xtremehobo 20 years, 9 months ago

    CUSTOMVERTEX m_Vertices[40000];
	long vcount=0;
    for(int x=0;x<201;x++)
	{
		for(int z=0;z<201;z++)
		{
			m_Vertices[vcount].x     = x;
			m_Vertices[vcount].y     = Heightmap[x][z];
			m_Vertices[vcount].z     = z;
			m_Vertices[vcount].color = 0xffffffff;
			m_Vertices[vcount].tu    = 1;
			m_Vertices[vcount].tv    = 1;
			vcount++;
		}
	}
When I try to run this code, I get an access error, so I run it in debug mode and check the value of vcount and its 1128726528 which seems to be impossible for two nested for loops that go up to 100 to reach. The max that vcount can reach should be 40,000 shouldn''t it? Anyways, why is this code giving me an access error. Specifically , it points to the line "m_Vertices[vcount].y = Heightmap[x][z]; but I''m kinda thinking the problem with vcount is causing it.. Thanks ----- GSACP: GameDev Society Against Crap Posting To join: Put these lines in your signature and don''t post crap!
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
Advertisement
You''re accessing 201x201 = 40401 vertexes.
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
the loop doesn't go up to 201 though.. it only reaches 200 (hence the x<201)

EDIT: nevermind, that solves it! (and about another 100 c++ problems I've had in the past)
lol thanks

[edited by - Xtremehobo on July 3, 2003 1:33:04 PM]
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
crispy is correct.

0..199 is 200 units across, "0" represents the first element, while 199 represents the 200th (last) element

so "< 200" should work, but not "< 201".

www.cppnow.com
I''ve already answered it for you on IRC, but bleh:

The condition section of your for loops should be "x < 200", as you wish to loop through 200 * 200 (40,000) of the arrays elements. As you have got it, it is looping round 201 * 201 (40,401) times.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]

This topic is closed to new replies.

Advertisement