C/C++ static vs global

static has a very simple logic to it. If a variable is static, it means that it is a global variable, but it‘s scope is limited to where it is defined (i.e. only visible there). For example:

  • Outside a function: global variable but visible only within the file (actually, the compilation unit)
  • Inside a function: global variable but visible only within the function
  • (C++) Inside a class: global variable but visible only to the class

Now let‘s see what the C11 standard says regarding static and extern (emphasis mine):

6.2.2.3

If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

6.2.2.4

For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.

6.2.2.7

If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.

So the standard says that first, if you have:

static int m;
extern int m;

then the second declaration (with extern) would regard the first one and in the end m would still be static.

However, in any other case, if there are declarations with both internal and external linkage, the behavior is undefined. This actually leaves us with only one option:

extern int m;
static int m;

i.e., extern declaration before static declaration. gcc was nice enough to give you error in this case of undefined behavior.

时间: 2024-10-20 01:45:14

C/C++ static vs global的相关文章

php中变量引用&不可与global共同使用

问题来源,新公司debug. 程序中代码大致如下 class Ci { private static $instance = NULL; public $name = 'hello'; public $load = null; public function __construct() { self::$instance = &$this; $this->load = new Load; mlog(self::$instance, __LINE__); //other code .... $

第三百七十节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索结果分页

第三百七十节,Python分布式爬虫打造搜索引擎Scrapy精讲-elasticsearch(搜索引擎)用Django实现搜索结果分页 逻辑处理函数 计算搜索耗时 在开始搜索前:start_time = datetime.now()获取当前时间 在搜索结束后:end_time = datetime.now()获取当前时间 last_time = (end_time-start_time).total_seconds()结束时间减去开始时间等于用时,转换成秒 from django.shortcu

linux 信号处理

前言 Linux中的信号是向进程异步发送的事件通知,通知进程有事件(硬件异常.程序执行异常.外部发出信号)发生.当信号产生时,内核向进程发送信号(在进程所在的进程表项的信号域设置对应于该信号的位).内核处理一个进程收到的信号的时机是在一个进程从内核态返回用户态时,当一个进程在内核态运行时,软中断信号并不立即起作用,要等到将返回用户态时才处理,进程只有处理完信号才会返回用户态,进程在用户态下不会有未处理完的信号.内核为每个进程维护一个(未处理)的信号队列,信号产生后首先被放入到未决队列中,如果进程

Web 在线文件管理器学习笔记与总结(13)重命名文件夹(14)复制文件夹

(13)重命名文件夹 ① 重命名文件夹通过 rename($oldname,$newname) 实现 ② 检测文件夹名是否符合规范 ③ 检测当前目录中是否存在同名文件夹名称,如果不存在则重命名成功 index.php: <?php require 'dir.func.php'; require 'file.func.php'; require 'common.func.php'; $path = 'file'; $path = @$_REQUEST['path'][email protected

存储持续性、作用域和链接性

作用域scope描述了名称在文件(翻译单元)的多大范围内可见. 链接性linkage描述了名称在不同单元间共享.链接为外部的名称可以在文件间共享,链接性为内部的名称只能由一个文件中的函数共享.自动变量的名称是没有链接性,因为他们不能共享. 在名字空间中声明的变量的作用域为整个名称空间,因此全局作用域是名称空间作用域的特例. 寄存器变量是一种形式的自动变量,因此其存储持续性为自动,作用域为局部,但没有链接性.关键字register提醒编译器,用户希望它通过使用CPU寄存器,而不是堆栈来处理特定的变

虚拟内存地址(翻译)

Virtual Memory and Memory Mapping Virtual memory was introduced in 1959. The main goal was to significantly simplify programming by abstracting the quirks of physical memory's hierarchy. Nowadays, virtual memory management is an essential component i

Java监控工具介绍,VisualVm ,JProfiler,Perfino,Yourkit,Perf4J,JProbe,Java微基准测试

本文是本人前一段时间做一个简单Java监控工具调研总结,主要包括VisualVm ,JProfiler,Perfino,Yourkit,Perf4J,JProbe,以及对Java微基准测试的简单介绍,总结下来比较推荐,Visualvm,Perfino,yourkit和Jprofiler(推荐值按顺序依次递减),其它工具不太推荐使用.下面是文章目录结构:  一.VisualVm  1.简介 2 2.安装插件并启动VisualVM 2 3.内存分析 3 4.CPU分析 5 5.线程分析 6 6.连接

express+gulp构建项目(五)swig模板

这里的文件负责配置swig模板引擎. index.js var jsonHash = require('./json_file'); var staticTag = require("./tag-static"); exports.init = function (swig) { swig.setExtension('static', function (input) { //swig.setExtension为自定义标签添加扩展.'static'为自定义标签的名字 //input是通

JS通过设置cookie来控制弹出层,首次打开页面显示弹出层

<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>JS通过设置cookie来控制弹出层,首次打开页面显示弹出层,刷新页面不再显示:关闭浏览器重新打开.清除缓存cookie显示.</title&