C ++ Storage Class: लोकल, ग्लोबल, स्टेटिक, रजिस्टर और थ्रेड लोकल

इस लेख में, आप C ++ में विभिन्न स्टोरेज कक्षाओं के बारे में जानेंगे। अर्थात्: स्थानीय, वैश्विक, स्थिर स्थानीय, रजिस्टर और धागा स्थानीय।

C ++ के हर वेरिएबल में दो विशेषताएं होती हैं: टाइप और स्टोरेज क्लास।

प्रकार डेटा के प्रकार को निर्दिष्ट करता है जिसे एक चर में संग्रहीत किया जा सकता है। उदाहरण के लिए: int, float, charआदि

और, भंडारण वर्ग एक चर के दो अलग-अलग गुणों को नियंत्रित करता है: जीवनकाल (यह निर्धारित करता है कि चर कितनी देर तक मौजूद हो सकता है) और गुंजाइश (यह निर्धारित करता है कि कार्यक्रम का कौन सा हिस्सा इसे एक्सेस कर सकता है)।

एक चर के भंडारण वर्ग के आधार पर, इसे 4 प्रमुख प्रकारों में विभाजित किया जा सकता है:

  • स्थानीय चर
  • वैश्विक चर
  • स्थैतिक स्थानीय चर
  • चर रजिस्टर
  • थ्रेड लोकल स्टोरेज

स्थानीय चर

किसी फ़ंक्शन के अंदर परिभाषित एक चर (ब्रेसिज़ के बीच फ़ंक्शन बॉडी के अंदर परिभाषित) को स्थानीय चर या स्वचालित चर कहा जाता है।

इसका दायरा केवल उस फ़ंक्शन तक सीमित है जहां इसे परिभाषित किया गया है। सरल शब्दों में, स्थानीय चर मौजूद है और इसे केवल एक फ़ंक्शन के अंदर ही एक्सेस किया जा सकता है।

फ़ंक्शन से बाहर निकलने पर स्थानीय चर का जीवन समाप्त हो जाता है (यह नष्ट हो जाता है)।

उदाहरण 1: स्थानीय चर

 #include using namespace std; void test(); int main() ( // local variable to main() int var = 5; test(); // illegal: var1 not declared inside main() var1 = 9; ) void test() ( // local variable to test() int var1; var1 = 6; // illegal: var not declared inside test() cout << var; )

चर var का उपयोग अंदर नहीं किया जा सकता है test()और var1 का उपयोग main()फ़ंक्शन के अंदर नहीं किया जा सकता है ।

autoइससे पहले भी स्थानीय चर को परिभाषित करने के लिए कीवर्ड का उपयोग किया गया था:auto int var;

लेकिन, C ++ 11 autoका एक अलग अर्थ है और इसका उपयोग स्थानीय चर को परिभाषित करने के लिए नहीं किया जाना चाहिए।

वैश्विक चर

यदि किसी चर को सभी कार्यों के बाहर परिभाषित किया जाता है, तो इसे वैश्विक चर कहा जाता है।

एक वैश्विक चर का दायरा पूरा कार्यक्रम है। इसका मतलब है, इसका इस्तेमाल किया जा सकता है और इसकी घोषणा के बाद कार्यक्रम के किसी भी हिस्से में इसे बदला जा सकता है।

इसी तरह, कार्यक्रम समाप्त होने पर ही इसका जीवन समाप्त होता है।

उदाहरण 2: वैश्विक चर

 #include using namespace std; // Global variable declaration int c = 12; void test(); int main() ( ++c; // Outputs 13 cout << c < 

Output

 13 14

In the above program, c is a global variable.

This variable is visible to both functions main() and test() in the above program.

Static Local variable

Keyword static is used for specifying a static variable. For example:

… int main() ( static float a;… ) 

A static local variable exists only inside a function where it is declared (similar to a local variable) but its lifetime starts when the function is called and ends only when the program ends.

The main difference between local variable and static variable is that, the value of static variable persists the end of the program.

Example 3: Static local variable

 #include using namespace std; void test() ( // var is a static variable static int var = 0; ++var; cout << var << endl; ) int main() ( test(); test(); return 0; )

Output

 1 2

In the above program, test() function is invoked 2 times.

During the first call, variable var is declared as static variable and initialized to 0. Then 1 is added to var which is displayed in the screen.

When the function test() returns, variable var still exists because it is a static variable.

During second function call, no new variable var is created. The same var is increased by 1 and then displayed to the screen.

Output of above program if var was not specified as static variable

 1 1

Register Variable (Deprecated in C++11)

Keyword register is used for specifying register variables.

Register variables are similar to automatic variables and exists inside a particular function only. It is supposed to be faster than the local variables.

If a program encounters a register variable, it stores the variable in processor's register rather than memory if available. This makes it faster than the local variables.

However, this keyword was deprecated in C++11 and should not be used.

Thread Local Storage

Thread-local storage is a mechanism by which variables are allocated such that there is one instance of the variable per extant thread.

Keyword thread_local is used for this purpose.

Learn more about thread local storage.

दिलचस्प लेख...