#### Archived

This topic is now archived and is closed to further replies.

# FOR loop acting strange

This topic is 5647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

    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!

##### Share on other sites
You''re accessing 201x201 = 40401 vertexes.

##### Share on other sites
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]

##### Share on other sites
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

##### Share on other sites

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 ]

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633766
• Total Posts
3013732
×