Τάξη αποθήκευσης C ++: Τοπικό, Παγκόσμιο, Στατικό, Μητρώο και Τοπικό νήμα

Σε αυτό το άρθρο, θα μάθετε για διαφορετικές τάξεις αποθήκευσης στο 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.

ενδιαφέροντα άρθρα...