储存类别 [英] storage classes

查看:74
本文介绍了储存类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明为autostatic的变量之间有什么区别? autostatic变量中的内存分配有什么区别? 为什么我们将static与指针数组一起使用,其含义是什么?

What is the difference between a variable declared as an auto and static? What is the difference in allocation of memory in auto and static variable? Why do we use static with array of pointers and what is its significance?

推荐答案

自动(默认)静态外部注册是C中变量的4个修饰符.

AUTO (default), Static, Extern & Register are the 4 modifiers for a variable in C.

  • 自动:默认值.普通变量.

静态:更改变量的生存期. (保留范围,否 改变).

STATIC : Changes the lifetime of the variable. (retains the scope, no change).

这意味着在运行时,操作系统不会从内存中删除该变量(一旦包含该变量的函数退出),并在每次调用该函数时初始化该变量.

This means, during runtime, the OS does NOT of delete the variable from memory once the function( containing the variable exits) and initialise the variable every time the function is called.

相反,仅在函数(包含它的函数)第一次被调用时才初始化静态变量.然后它将继续驻留在内存中,直到程序终止. 换句话说, STATIC 可有效地在内存中创建变量GLOBAL,但只能进行本地访问.

Rather the static variable is initialised ONLY the first time the function (containing it is called). Then it continues to reside in the memory until the program terminates. in other words STATIC effectively makes a variable GLOBAL in memory, but with only LOCAL access.

静态变量的存储位置取决于它们是否被初始化为0.

Where your statics are stored depends on if they are 0 initialized or not.

  • 0个初始化的静态数据进入.BSS(以符号开头的块),

  • 0 initialized static data goes in .BSS (Block Started by Symbol),

非0初始化数据进入.DATA

non 0 initialized data goes in .DATA

必须注意,尽管静态变量始终位于内存中,但是只能从本地范围(定义它们的功能)中访问它们.

One must note that, though static-variables are always in the memory, they can only be accessed ONLY from the local scope (function they are defined in).

EXTERN:用于向编译器发出信号,extern-definition只是一个占位符,而实际定义在其他地方.将变量声明为extern会导致您的程序在声明该变量的范围内不为该变量保留任何内存.找到声明为extern的函数原型也很常见.

EXTERN : Used to signal to the compiler that the extern-definition is simply a placeholder, and the actual definition is somewhere else. Declaring a variable as extern will result in your program not reserving any memory for the variable in the scope that it was declared. It is also common to find function prototypes declared as extern.

REGISTER:指示编译器最好使用CPU寄存器(而不是RAM)来存储此变量. 重复访问变量时(例如:循环计数器变量),用于提高性能.

REGISTER : Signals the compiler to Preferrably used a CPU-register (and not RAM) to store this variable. Used to improve performance, when repeated access to the variable is made (for ex: loop counter variables).

这篇关于储存类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆