Thread: C global variables

  1. #16
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    I thought so too but I wasnt sure.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  2. #17
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    hahaha it happens. I was just scratching my head for a bit. That was nasty of you .... making me think on a sunday
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    Moreover, if you want to redeclare the variable in the main function, just rewrite the prototpe for that function like so:

    void usage(char **err_msg);

    Treat it the same as you already are inside the function.

    Anyway, there is no good reason for that type of function!

    The only time you should use this type is when:

    1) Inside the function there is a static variable that you want initialized on the first call only.

    2) You wish to keep track of the number of times a function is called

    3) Other Valid Uses


    int Allocate(char *string, int len)
    static int timesInvoked = 0; //initialized to '0' first call only...
    static int memoryAlloced = 0; //initialized to '0' first call only...

    char *resource = (char *)malloc(sizeof(char) * len);

    return 0;

    memset(resource, 0, len);

    memoryAlloced += len;

    if(memoryAlloced > 1000000)
    puts("Careful! Memory Usage Is High!!");

    printf("Total Memory Allocated = %i \n\n",memoryAlloced);
    printf("Function Called %i Times \n\n\n",timesInvoked);

    return 1;
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  4. #19

    Variable Allocation- The FACTS

    Okay, let's clear the air on this issue first. Most of you don't know how a compiler puts your code together, so you get easily confused on what and where and how. It's understandable as this stuff just isn't available any more.

    With that said, let's ask ourselves just _what_ is a global variable? Basically, when a global variable is declared, it tells the compiler that you want a piece of RAM allocated for the term of the applications' operation.

    In otherwords, you want a storage space of arbitrary size kept around until the program quits. Why? because you want every part of your program to have access to that info.

    When?-- It is allocated at compile time. It is allocated as part of the exectuable binary (it doesn't execute, but it does increase the size of your executable).

    It is _NOT_ allocated on the fly.

    If I create a global variable in my program 'char a[100];', I have just told the compiler to 'set aside' 100 bytes of RAM for exclusive reference by this variable.

    As such, during link (at compile time), wherever the global variable space is determined to reside, it will tack on an extra 100 bytes of space and whenever you reference the array 'a', it will always point to this location.

    That is the primary reason why it is better to allocate large chunks of RAM dynamically (during execution time), because at any one point in time, it gives you more control of your heap space.

    What about 'static' global variables?

    They are considered global and are allocated exactly the same way as true global variables. The difference is that they are not linked the same, so they are not accessible by other source files.

    What about strings and constants?

    These are also allocated at compile time and placed in the 'data' area of the executable, along with the globals. That's why if your program is wrong and a global variable is overwritten or stomped on, the error may show up by damaging the contents of string or constant data.


    Big Question: Can I declare a global variable anywhere within my sourcecode?

    Yes... but. Your compiler does not CARE where the global variable is declared, so long as it meets all the syntactic and grammatic qualifications. However, I always allocate all globals either in their own sourcefile 'xxGlobals.c' (with it's own header 'xxGlobals.h') or I declare them at the head of main, just before the functions/procedures are declared.

    The reason I do that is because it's organized. Scattering global variables throughout your code is 'sloppy', and discouraged.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 01:59 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM