Connecting Tech Pros Worldwide Forums | Help | Site Map

storage class

Newbie
 
Join Date: Apr 2007
Posts: 1
#1: Apr 30 '07
hi ,


What is the region that , local variables store garbage value and global variables store 0 value

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#2: Apr 30 '07

re: storage class


Quote:

Originally Posted by yogeshchindejain

What is the region that , local variables store garbage value and global variables store 0 value

I assume you mean "What is the reason that ..."?

In a word Speed.

Global variables are initialised once at startup and the performance hit of iniitialising them is quite low.

Local variables used shared memory from the stack so would need to be initialised every time the function was called, this would cause quite a performance hit and given that in many cases the program would then change the value to something else before using it it just isn't worth doing.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: Apr 30 '07

re: storage class


I do confess that in the old days when the world was just VAXen I did things
similar to this:
Expand|Select|Wrap|Line Numbers
  1. void foo() {
  2.    char* p= "gotcha!";
  3. }
  4. void bar() {
  5.    char* q;
  6.    puts(q);
  7. }
  8. int main() {
  9.    foo();
  10.    bar();
  11.    return 0;
  12. }
I don't do that anymore, I grew older too ... ;-)

kind regards,

Jos
Reply