I'm using VC++ 2010 with Windows 7 x64. I'm compiling all my code as C code.

I have a for loop like this:

for (i = 0; i < max; i++)
{
    ...
}

But when I run it, i is being set to max

If I set i to 0 in the first line of the for loop, it does change to 0, but at the end of the for loop, it does not go back up and check the condition, it just exits out of the loop.

Setting i to 0 before the start of the for loop does not change this, neither does using an entirely different counter variable.

Here is the actual code being used:

indices = malloc (sizeof(short) * data.face_count * 3);
for (i = 0; i < data.face_count; i += 3);
{
    indices[i]     = data.face_list[i]->vertex_index[0];
    indices[i + 1] = data.face_list[i]->vertex_index[1];
    indices[i + 2] = data.face_list[i]->vertex_index[2];
}

EDIT: Changing it to a while loop makes it function normally, so it's something to do with that paticular for loop (all others work correctly).

Edited 4 Years Ago by tomtetlaw

I have a for loop like this:

   for (i = 0; i < max; i++)
    {
     ...
    }

But when I run it, i is being set to max

If I set i to 0 in the first line of the for loop, it does change to 0, but at the end of the for loop, it does not go back up and check the condition, it just exits out of the loop.

Then max is less than 2.

max in the actual code is 12. and even if i is a value different to 0 before this loop, it always gets changed to max
btw it gets changed to max before the first line of code of the first run of the loop

Edited 4 Years Ago by tomtetlaw

Well, based on the code you posted, there is nothing wrong. So, either
1) you're are not understanding what you are seeing
2) the problem is not in the loop
3) you have a definition somewhere that's wrong and you're trouncing memory you shouldn't be touching.

Try adding printf() statements to check values at key places in the program.

What I don't understand is why i is not being set to 0 when it first gets to the for loop.
I'll look around for things that could be overwriting memory.

indices = malloc (sizeof(short) * data.face_count * 3);
for (i = 0; i < data.face_count; i += 3);
{
    indices[3 * i]     = data.face_list[i]->vertex_index[0];
    indices[3 * i + 1] = data.face_list[i]->vertex_index[1];
    indices[3 * i + 2] = data.face_list[i]->vertex_index[2];
}

Multiply i by 3. Try it. You'll like it.

Well, DeanMSands3 was right about one thing: the original loop was erroneous. The correct solution is this:

indices = malloc (sizeof(short) * data.face_count * 3);
for (i = 0; i < data.face_count; i++);  // notice i++ and not i += 3
{
    indices[3 * i]     = data.face_list[i]->vertex_index[0];
    indices[3 * i + 1] = data.face_list[i]->vertex_index[1];
    indices[3 * i + 2] = data.face_list[i]->vertex_index[2];
}

As for the weird behavior, it looks like a memory corruption to me. I would start by making sure the code is correct (like the above), and then start talking about "weird" behavior. In my experience, compilers (almost) never act in a weird way, but programmers do all the time!

This article has been dead for over six months. Start a new discussion instead.