Can I define a global variable in a "if" loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anton Andreev
    New Member
    • Jul 2011
    • 1

    Can I define a global variable in a "if" loop?

    First of all I'll give a contest as it seems I'm trying to make something inappropriate.

    The aim is to add a new line to the array on each iteration of the loop.

    The loop is "implied" by "void CMyAppViw::OnLB uttonDown(...)"

    I've tried to make it in a number of ways but filed. Here is the recent one:
    Code:
    int n=1;
    int array[1][2][3];
    
    
    void CMyAppView::OnLButtonDown(UINT nFlags, CPoint point)
    {
        
        
        if (n==1)
        {
            calculations(array[],n);
        }
        if (n==2)
        {
            int temp_array[1][2][3]=array[1][2][3];
            //somehow destroy array
            int array [2][2][3];
            //call a function to copy temp[1][2][3] values to array [2][2][3]
            calculations(array[],n);
        }
    n++
    }


    Eventually here are the questions:

    1. how to initialize a global variable in if-loop?
    2. how to destroy a variable?
    Last edited by Niheel; Aug 5 '11, 03:53 AM. Reason: code tags
  • alexis4
    New Member
    • Dec 2009
    • 113

    #2
    1. A global variable is declared outside of functions and they are visible from all file functions. So you can't declare a global variable inside an if.

    2. Global variables cannot be destroyed, they have lifetime equal to the application's lifetime (that is until application exits). Auto variables have lifetime equal to function's lifetime except main's auto variables, that have lifetime equal to the application's lifetime.

    Code:
    int array[1][2][3];
    int temp_array[1][2][3]=array[1][2][3];
    At line 2 your array variable is out of range.


    Alex

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      what is an if loop?
      @alexis4 the OP is talking about initializing a global variable not declaring it. If the global variable was not initialized(whi ch is bad practice) then it could be initialized in the body of an if()statement.
      also arrays cannot be assigned like this
      Code:
      int temp_array[1][2][3]=array[1][2][3];
      each element in the original array has to assigned individually to each new element in the new array. on the other hand vectors can be assigned.
      a variable can be created on the heap with new and destroyed with delete.

      Comment

      • alexis4
        New Member
        • Dec 2009
        • 113

        #4
        @alexis4 the OP is talking about initializing a global variable not declaring it. If the global variable was not initialized(whi ch is bad practice) then it could be initialized in the body of an if()statement.

        Hi whodgson!

        I understood the question, but my answer is still that initialization of a global variable in if() does not even exist as term, at least according to my compiler. All global variables are placed in the zero initialized section, unless they are initialized by user (ie int a=5), or a directive is given that this is a non initialized global variable.
        Either way, initialization is done already before code has reached if(). I 'm saying again that this stands for my compiler.


        Alex

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Hi Alex
          OK I was aware that string global variables were initialized to '\0'
          but I was not aware that ALL global variables were initialized to '\0'
          Thanks for sorting me out.

          Comment

          Working...