Advertisement

weird variable problem

Started by January 22, 2001 11:41 AM
10 comments, last by Thrump 24 years ago
I''m getting a really weird problem in my code. It has to do with a variable in a local to a function that''s not even being called. Here''s an example. void function_that_is_never_called(void){ int variable; if(condition) variable = 5; //variable = 0; if(variable == value) statement; } ok.... the problem is, this code introduces slowdown in the engine. I am positive the function is not called, which is the weird part. If I put in the commented out line, it runs fine, but if it''s commented out, it runs slowly. If I take out just the top condition, it runs smoothly. If I take out the bottom condition, it runs fine. The weird thing is, this code is never actually executed in the program, so, lines of code that are not being executed are changing how the program runs. Any ideas?
What if you comment out the whole function?
Advertisement
Getting rid of the function make it run fast again. Bah! ???
I don''t know about you, but personally I prefer not to have functions I don''t use to clutter my code, besides it''s the best way to really make sure that you aren''t using the function.
Besides you have to give more info/source, I doubt anyone could figure out what is wrong with that vague example.
The only thing I could think of right out of my head without seeing more code is that it''s about some cache-miss or byte-alignment in the code, but I doubt that. Still since I only have your ''it runs slowly'' to measure how slowly it runs, it''s quite hard to say what it is.
Well, this is only a guess, as I don''t know more about your code, but here''s what I think is going on.

Due to the way in which they access memory, some processors run faster when accessing memory (or running instructions in locations) that is (are) word-aligned. My guess is that, by adding that extra line (or fiddling with it in various other ways) you''ve managed to add or remove enough code in the compiled version to word-align your program, making your code run faster.

As for a solution? You''ve discovered the behavior that makes it run smoother, so leave that line in.

Props to Michael Abrash for the information, btw.

Adiss
a.k.a. Magic Card
I know this will totally annoy everybody, but... the function is needed. Which is why it''s so frustrating that it runs smoothly without it. As for the assignment line, this was a debug line, which has to be removed. Hmmm, maybe I''ll try going variable=variable, so the value doesn''t change... maybe that will work. Still, it would really be nice to know why it slows it down to %50 speed, as the problem will probably come up again. Oh, it has something to do with another function that draws lines. Whenever the camera faces one of these lines, it gets the slowdown. I''m not sure if putting these lines in just pushes it a tiny step further so the the screen refresh isn''t met, or if somehow the variable affects this function. Anyway, thanks for the help. I really have no idea what the problem is, so I hope I''ll wake up some day and it will be gone.
Advertisement
you smoke crack. scope , scope , scope
Have you commented out the function just to make sure you don''t call it in some code you last visited 14 days ago and have forgotten all about? Just see if it compiles.
What kind of program is it and what does this evil function do?
Anon, do I have bad breath, or do you think it''s a global variable thing? It doesn''t compile if I don''t declare it, and renaming it to something crazy doesn''t help either. Is that what you meant? Yes, it compiles totally commented out, and I think it ran good. I do call the function in my code, but I''m positive the line never gets executed. A certain key has to be pressed for it to be called, and I don''t push it. Lot''s of printf''s are in the function too, and they aren''t printed, so I''m pretty sure it''s not being called. I was going to post that yesturday, but www.gamedev.net doesn''t work half the time for me. And, crazier and crazier, commenting out the call fixes the problem. (I''m still positive it''s not being called) I''m thinking it''s a memory alignment thing like somebody said, and the combination of that and the line drawing slows it down enough to bring it from one Htz bracket to the next. The function splices together motion captures... the varible is an int that gives the general direction the character is supposed to move (0=forward for example). Now, to make the problem fun, I''ve been changing the function around a bit, and now it runs fine without the trouble line. Hopefully it will stay fixed. I''d love to know the problem though.
I think your problem stems from not initialising the variable before you use it. ok, you''re initialising it in the first if statement, though only if the condition is met. If the condition is not met then the variable could be any random value, when this is used in the second if statement, the program has a hard time determining the result.

This topic is closed to new replies.

Advertisement