我们知道,当控件从函数中退出时,堆栈空间将被释放。那么静态变量会发生什么。它们会保存在任何内存中并在调用函数时检索吗??
最佳答案
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/