-
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
-
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
-
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?
-
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
-
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.
-
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.
-
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
-
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.
-
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.
-
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
-
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
-
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 :).
-
why?
-
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
-
Oops, I really misread the declaration for err_msg. **** happens.