c - 如何为每个函数调用检索静态变量

标签 c function static

我们知道,当控件从函数中退出时,堆栈空间将被释放。那么静态变量会发生什么。它们会保存在任何内存中并在调用函数时检索吗??

最佳答案

wiki说:

In the C programming language, static is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.

Static local variables: variables declared as static inside a function are statically allocated while having the same scope as automatic local variables. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again.

关于c - 如何为每个函数调用检索静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37564582/

相关文章:

在 C 中连接两个字符串并将结果放入第三个字符串

c - 以随机顺序读取文件

c - 可能涉及范围的段错误问题

Javascript if/else 提交

c++ - 在 .exe 应用程序中加载 C++ DLL

c - 如何理解printf(str+9)的语句?

C 的未定义行为

javascript - 用于单个 div 的 jquery 函数

c# - 保存用户消息的静态字典

c - 访问和修改局部静态变量