Scope error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • avi05
    New Member
    • Sep 2009
    • 2

    Scope error

    void func1(void)
    {

    strans obj;\\\\struct
    unsigned int i,ix;
    ix=1;

    if (a>0)\\\\\\\\\\ \\\a is global int
    ix=a;

    for (i=ix;i<5;i++)
    {
    func2(i,obj);
    \\\\\\\\\\\\\\\ \\\\\\\i value here i =1 ok
    func3(obj);
    \\\\\\\\\\\\\\\ \\\\\\\ func3 ok
    \\\\\\\\\\\\\bu t after this func3 call i value===0\\\\\e rror
    \\not making any change to or passing address of i in func3 but still getting \\reset
    \\if i use static for i variable it works correctly
    \\but scope of i even though local is through out function1 is it not?
    \\please tell me why this problem occurs..


    }

    }

    could it be a stack problem were i is lost when func3 is called ,,isnt static used the other way around to retain i value when flow exits out of func1..thankyou for all posts
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You probably have a problem in func3, some sort of out of bounds write to a local array variable in func3 could corrupt the local data in func1.

    Comment

    • avi05
      New Member
      • Sep 2009
      • 2

      #3
      thankyou bafta i am using a gnu cross compiler when i got this error ,but when the same code is ported to borland bc5 it works correctly...cou ld it be compiler optimisation in bc5 that is avoiding this out of bounds write into array...

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        It may be anything - endianness, word/dword alignment, difference in size of int/long types. Try to pad i,ix declaration with some local arrays filled with some like 0xcc and see then what happens to them and how much of them is zeroed.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by avi05
          thankyou bafta i am using a gnu cross compiler when i got this error ,but when the same code is ported to borland bc5 it works correctly...cou ld it be compiler optimisation in bc5 that is avoiding this out of bounds write into array...
          If you are writing outside of an array bounds then you have invoked undefined behaviour.

          Anything can happen with undefined behaviour including the program appearing to work and it is likely that different things would happen on different platforms so just because your code appears to work in Borland BC5 doesn't mean that there is no error in the code. It just means BC5 is reacting to undfined behaviour in a different way to the gnu cross compiler.

          You need to check the code of func3 and see if you can see the error.

          Comment

          Working...