C global variables

This is a discussion on C global variables within the C Programming forums, part of the General Programming Boards category; I was wondering if anyone can answer my question. I am working on some code that has too many global ...

  1. #1
    Dohojar
    Guest

    C global variables

    I was wondering if anyone can answer my question.
    I am working on some code that has too many global variables. my question is this, Most of the globals are in .c file(why I have no idea I never wrote the code originally) and I have encountered so globals in the middle of the .c file. I know the scope of the globals but when is memory set aside for globals in .c files. is it at run time when the program starts or when the global is first used.
    Any help would be appreciated

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    When the program execution reaches a global variable it will allocate space for it and initialize it to the equivalent of all bits zero for the data type of the variable.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Dohojar
    Guest

    So basically....

    So basically the compiler knows about it but wont allocate it till the first time you use the variable when the program is running?
    Is that how it works?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    No, the memory is allocated as soon as program execution reaches the declaration, for example:
    Code:
    #include <stdio.h>
    
    int i; /* i now exists and takes up an ints worth of space in memory */
    char *ch; /* ch exists and takes up a char pointers worth of memory */
    /* Both variables can be used at this point */
    
    int main ( void ) 
    {
    -Prelude
    My best code is written with the delete key.

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    For most systems local variables are stored on the stack and global variables are stored in the data segment of the program. Globals are pre-intialized even before the program runs. It gets more complicated when you throw in c++ static objects and constructors. So the memory is set aside in the executable before the program runs though your compiler isn't going to let you refer to globals undeclaired.

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Be careful with too many global variables. For one, too many (I mean a lot) can be bad for memory management. Secondly, it's really...really hard to debug with many of global variables to find out where they go, what they're for, etc.
    1978 Silver Anniversary Corvette

  7. #7
    Dohojar
    Guest

    re: globals

    Yea I know they are really bad to use and that is what I am trying to rid the code I am working on of. The original programmer loved to use them in the middle of c files that dont even contain the main(). Quite annoying really. What I am trying to get rid of is
    an array of char pointer that is used just to hold a display msg for a function that that will display this msg like so,

    char *err_msg[] =
    { // there are like 20 strings in here }

    void usage(void)
    {
    int i;
    for(i=0;i<whatever;i++)
    fprintf(stderr,"%s\n",err_msg[i]);
    }

    Why it wasnt just done inside the function is beyond me but what I wanted to know was if this was allocated memory for this before it was used and I guess it is. just only this function can see it.
    thanks for all the help

  8. #8
    Unregistered
    Guest
    With the way the codes is written

    Code:
    char *err_msg[] = {"Hello", "Bye", "To day"};
    
    void usage()
    {
        int i;
        for(i = 0; i < 3; ++i)
    	printf("%s\n", err_msg[i]);
    }
    compiles to

    Code:
    .data
    	.align 4
    	.type	 err_msg,@object
    err_msg:
    	.long	.LC0
    	.long	.LC1
    	.long	.LC2
    		.section	.rodata
    .LC2:
    	.string	"To day"
    .LC1:
    	.string	"Bye"
    .LC0:
    	.string	"Hello"
    	.size	 err_msg,12
    .LC3:
    	.string	"%s\n"
    The array err_msg is stored statically along with the string data. The .globl directive gives err_msg global linkage. Probably the easiest way to fix this is to code it like

    Code:
    static char *err_msg[] = {"Hello", "Bye", "To day"};
    
    void usage()
    {
        int i;
        for(i = 0; i < 3; ++i)
    	printf("%s\n", err_msg[i]);
    }
    Or you could put err_msg inside usage.

  9. #9
    Dohojar
    Guest

    Question re: static

    So what will the keyword static do in this case? I know it will limit the scope of the variable in this case. Is there something I am missing?
    I was just going to put it in the usage function.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The static keyword when used in global scope means that the variable can only be used in that file. So you can't use the variable by declaring it as extern in another source file.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Dohojar
    Guest

    Talking

    Ahhh. That makes sense, thanks. Only one file will see it but the memory will still be set aside for it. Damn variable should never have been used in the first place as far as I am concerned.
    thanks for all the help

  12. #12
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs up

    Just for info:

    This code

    Code:
    void usage(void) 
    { 
    int i; 
    for(i=0;i<whatever;i++) 
    fprintf(stderr,"%s\n",err_msg[i]); 
    }
    is wrong.

    It should be like this

    Code:
    void usage(void) 
    { 
    fprintf(stderr,"%s\n",err_msg); 
    }
    or like this

    Code:
    void usage(void) 
    { 
    int i; 
    for(i=0;i<whatever;i++) 
    fprintf(stderr,"%c\n",err_msg[i]); 
    }
    Have fun .
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  13. #13
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115

    why?

    why is it wrong?
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It's not wrong, I think GaPe misread the declaration for err_msg. It is declared as an array of char pointers, not a single char pointer.

    -Prelude
    My best code is written with the delete key.

  15. #15
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs down

    Oops, I really misread the declaration for err_msg. **** happens.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 10:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 02: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, 05:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21