Search code examples
c++cfunctionmemoryglobal-variables

Does the use of a global variable within a function cause a local copy to be created


Unfortunately I can't find anything on the subject although I can't believe this question hasn't been asked before.

When I use a global variable inside a function, is a local copy of the variable created as with a passed argument or is the global variable accessed directly? Does it even make sense to define a global variable as a parameter of a function using the reference declarator & or as a pointer *?

PS: I know that global variables are a bad practice, but I'm programming for a microcontroller where it sometimes make sense to use global variables.


Solution

  • When I use a global variable inside a function, is a local copy of the variable created as with a passed argument or is the global variable accessed directly?

    The global variable is used directly. A local copy is not made.

    Does it even make sense to define a global variable as a parameter of a function using the reference declarator & or as a pointer *?

    Yes, there are some situations in which this may make sense. For example, if you have several global variables and have a function which writes to these global variables, but not always to the same global variable(s), then it may make sense to pass a pointer or a reference to a global variable.

    In the code block below, I have created a simple example program, which defines three global variables a, b and c and first swaps the values of the variables a and b and then the values of the variables b and c. For both swaps, the same function can be used, because the function takes as function arguments a pointer to each global variable that is to be swapped.

    It is necessary for the global variables to be passed by pointer (or in C++ by reference) instead of by value, because otherwise, the function would swap the local function arguments instead of the global variables.

    #include <stdio.h>
    
    int a = 5;
    int b = 3;
    int c = 2;
    
    void swap( int *first, int *second )
    {
        int temp = *first;
        *first = *second;
        *second = temp;
    }
    
    int main( void )
    {
        printf(
            "Before swapping,\n"
            "the global variables have the following values:\n"
            "a = %d\nb = %d\nc = %d\n\n",
            a, b, c
        );
    
        swap( &a, &b );
    
        printf(
            "After swapping the values of a and b,\n"
            "the global variables have the following values:\n"
            "a = %d\nb = %d\nc = %d\n\n",
            a, b, c
        );
    
        swap( &b, &c );
    
        printf(
            "After also swapping the values of b and c,\n"
            "the global variables have the following values:\n"
            "a = %d\nb = %d\nc = %d\n\n",
            a, b, c
        );
    }
    

    This program has the following output:

    Before swapping,
    the global variables have the following values:
    a = 5
    b = 3
    c = 2
    
    After swapping the values of a and b,
    the global variables have the following values:
    a = 3
    b = 5
    c = 2
    
    After also swapping the values of b and c,
    the global variables have the following values:
    a = 3
    b = 2
    c = 5
    

    Since you tagged the question with both C and C++ (which should generally not be done), I gave an example that is compatible with both C and C++. In C++, one would generally use a reference instead of a pointer, and one would use std::cout instead of printf.