Thread: Tips for finding scope problems

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    Tips for finding scope problems

    I have the following code here. My problem is that when the switch...case block hits nIDEvent == 1, SlavePos structure (which is an array of 6 floating point numbers) is modified by (0, +/- delta, 0, 0, 0, 0). But when the debugger hits the line "break;", SlavePos's values return to the original values before exiting the function. Does anyone know what type of scope problem that I'm running into in this case? Thanks in advance.

    Code:
    void InsertSimulGLrender::OnTimer( UINT_PTR nIDEvent )
    {
    	BrachySimApp *theApp = (BrachySimApp*) AfxGetApp();
    	float delta = 0.05;
    
    	switch(nIDEvent)
    	{
    	case 1:
    		// This is used to attempt to move the needle
    		if (lpressed)
    			theNeedle->SlavePos[1] -= delta;
    		if (rpressed)
    			theNeedle->SlavePos[1] += delta;
    		break;
    	case 2:
    		// some other handling code that is not relevant to the problem
    		break;
    	}
    	CView::OnTimer(nIDEvent);
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Are you sure it is being modified?
    Alternatively... are you sure it isn't being modified twice?

    Soma

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To me, scope problem means that you are hiding a variable or using the address of a variable that is out of scope, neither of which this appears to be from the posted code.

    What other code do you have that changes this variable?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Only the constructor makes changes to the array, and then it's never called again. I have a hard time finding what else is modifying the variable. I checked that the variable is modified within the if statements when appropriate, only to find out that they have been modified back again when the program hits "break;" in the debugger.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I think I have found out a potential culprit. delta is a float type. SlavePos is actually a FLOAT type. I'm not sure why they would make a difference, since FLOAT is a Windows typedef of float, but according to my observations, it did.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Are you by chance compiling with optimization?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    From what I see in the optimization settings in the Visual Studio project, no.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Scope in ActionScript
    By sean in forum Tech Board
    Replies: 4
    Last Post: 09-03-2008, 04:14 PM
  3. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM
  4. Question about C# scope rules
    By converge in forum C# Programming
    Replies: 3
    Last Post: 01-30-2002, 06:56 AM
  5. Returning references (scope problems?)
    By DarkDragon in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2001, 07:48 PM