Thread: Can someone explain to me

  1. #1
    rEtaRD r HUMANS TOO !!! rEtard's Avatar
    Join Date
    Feb 2005
    Posts
    80

    Can someone explain to me

    Code:
    void CalculateMousePoint(int MaxCount,int minX,int minY,int MouseX,int MouseY)
    {
    	int i=0; // for loop
    	int noteWidth = 54;
    	int noteHeight = 50;
    	static int maxX;
    	static int maxY;
    	while(i<MaxCount)
    	{
    		maxX = minX + noteWidth;
    		maxY = minY + noteHeight;
    		// check if mouse is within any note bitmaps
    
    		if ( MouseX > minX && MouseX < maxX && MouseY > minY && MouseY < maxY )
    		{
    			Call();
    			i = MaxCount;
    		}
    		else if ( maxX >= 307 )
    		{
    			minX = 125;
    			minY += noteHeight + 20;
    			i++;
    			// pls think of a loop to solve the problem
    			// this is for checking and increment max n min values
    		}
    		else
    		{
    			minX += noteWidth + 20;
    			i++;
    		}
    	}
    }
    alrite, i keep it short, this function is for looping through the coordinates of known starting X and Y axis, incrementing X most of the time and when X is at the max of Client area which is around 307, it will reset X to 125 and Y will increase. This function is used in WM_LBUTTONDOWN to correctly pinpoint whether an icon was clicked. Ok, here cums the problem.
    i started this function by declaring the variables as normal int maxX,maxY and the loop failed and even freeze when it reaches if(maxX >= 307), i narrowed the search and found the culprit was minY += iconHeight + 20; i figured for almost 2 days where did i do wrong and around 5 mins earlier, i added static into my variables and the loop worked, but the funny thing is maxX do not require static and it still worked ?!?!
    Can someone explain to me how does the static cum into place. Static is mainly used to retain the value within the variable but why doesn't maxX require it ???
    /* Have a nice day */

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The short answer, in bug-free code, it should make no difference at all.
    All that making it static does for you at the moment is put it in a different part of memory.

    Most likely, if you debug it carefully, you'll see your variable changing value when you least expect it.
    My idea is that there is a buffer overrun in some other function, and here is where you get to notice it. Rearranging the code rearranges the bug (so it doesn't happen here), but that's not to say that the bug has been fixed, or that something else unexpected won't happen somewhere else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Explain me few terms that i have listed in here.
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 08:20 AM
  2. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 05:23 AM
  3. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM
  4. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM