samedi 4 avril 2015

If statement being executed despite condition not being met

Ok, so, this is a bit of a weird issue to me. I have this portion of code in my program that uses an if statement to check and see if a page was found in memory, both of which are structs. However, I have an idea of what the output should be and my output is way off so I started checking through some areas to see where it could be coming from and in the process of doing that I ran into this issue. Here in this spot:



if(memory.memory[i].uid==position)
{
i=pages;
found=1; //The page was found in main memory
cout << memory.memory[i].uid << " " << position << endl;
}


The cout there is for testing purposes and is how I found this issue. The memory.memory[i].uid is always equal to 0, but the code inside the if statement runs anyway (so I get huge blocks of "0 ##" with ## increasing as it should be due to the position variable).


Is there any possible reason that would happen? It just doesn't make sense to me that the code inside the statement is being executed despite the statement being false. Both of the variables in the statement are integers.


(I'm not sure how much more of the code would be needed, it's a big program spanning 3 files so I didn't want to include all of it if I can avoid it. If more is needed though I can edit more in.)


EDIT: Wow, I'm blind. I just realized I'm modifying i before that statement so it's not the same i being used in the statement. After fixing it so the statement was before the modification of i the values are matching as they should be, sorry for the waste of time.


Aucun commentaire:

Enregistrer un commentaire