Understand the memory class of C language

In C language, variables have the idea of ** storage class ** and ** scope **. The table below shows this in the under-care.

Declarator Classification Under-care expression
auto Automatic variables Sister of cabaret club.
static Static variables Mistress on a business trip.
register Register variable The partner of the joint party.
extern Global variables My wife.

The storage class ** ʻextern` ** declares a ** global variable **. It's kind of like a ** real wife **, and it doesn't disappear anywhere in the country. Moreover, it can be accessed from anywhere (if you feel like it).

The storage class ** static ** declares a ** static variable **. It's kind of like a ** mistress ** on a business trip, and while it doesn't disappear, it's only accessible to the area. You'll have to go there to get access, but it's a great convenience to remember when you next visit and stay with you when you break up.

The storage class ** ʻauto` ** declares ** automatic variables **. It's kind of like ** Kyabakura's older sister *, and you can't meet unless you go there, but if you go, it will be secured automatically. However, I have completely forgotten the previous thing, and they are not the same person. ( I've never been to a cabaret club, so it's just an imagination)

The final storage class ** register ** declares a ** register variable **. This is like a ** joint party partner *, and whether it can be used or not depends on various factors. In many cases, the number of registers (≒ female) is overwhelmingly smaller than the number of variables (≒ male). It is often unsuitable for what you want, and it is common for you to already have an assignment. ( I've never been to a joint party, so it's just an imagination)

Let's dig a little deeper. Global and static variables do not disappear wherever you go. On the other hand, automatic variables exist only inside the function (≒ cabaret club). In principle, global variables and static variables refer to the same person if they have the same name. On the other hand, automatic variables do not always have the same name but the same person (≈ memory location).

What happens when you look at this from the point of view of scope? Let's say it's easy to understand and they all have the same name. Midori can be accessed from all over Japan, no matter where in the program the global variable Midori is declared, such as Tokyo. However, if the static variable Midori is declared in the file Hokkaido, the static variable Midori obscures the existence of the global variable Midori in Hokkaido. In other words, it will be forgotten. However, if Hokkaido has a cabaret club called Susukino and an automatic variable called Midori, the static variable Midori will also disappear. In other words, it will be forgotten. The global variable Midori in Tokyo is hazy in front of the static variable Midori in Hokkaido, and it is also hazy in front of Susukino's cabaret girl Midori. It's kind of scary, but it's a story that I don't really feel at all.

It's a tiring story, so let's leave it here. However, it is impossible, isn't it?

Recommended Posts