I thought so too but I wasnt sure. :D :D :D
Printable View
I thought so too but I wasnt sure. :D :D :D
hahaha it happens. I was just scratching my head for a bit. That was nasty of you .... making me think on a sunday:D
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
Like:
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);
if(!resource)
return 0;
memset(resource, 0, len);
memoryAlloced += len;
timesInvoked++;
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;
}
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.