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
externin 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.
C/C++ static vs global的更多相关文章
- static 和 global
global global关键字如果用在function内部,则说明这个function内用的这个变量是全局的,全局变量就是在整个页面里都能起作用.例如 $conf = 1; function con ...
- swoole为什么不建议使用static和global
$http = new swoole_http_server("0.0.0.0", 9501); $http->on("request", functio ...
- error X3025:global variables are implicitly constant, enable compatibility mode to allow modification
global variables are implicitly constant, enable compatibility mode to allow modification http://xbo ...
- php中变量引用&不可与global共同使用
问题来源,新公司debug. 程序中代码大致如下 class Ci { private static $instance = NULL; public $name = 'hello'; public ...
- 从DOM操作看Vue&React的前端组件化,顺带补齐React的demo
前言 接上文:谈谈我对前端组件化中“组件”的理解,顺带写个Vue与React的demo 上次写完博客后,有朋友反应第一内容有点深,看着迷迷糊糊:第二是感觉没什么使用场景,太过业务化,还不如直接写Vue ...
- [c++] Smart Pointers
内存管理方面的知识 基础实例: #include <iostream> #include <stack> #include <memory> using names ...
- [Scala] Scala基础知识
Object An object is a type of class that can have no more than one instance, known in object-oriente ...
- PHP变量作用域详解(二)
学过C的人用PHP的时候一般会相当顺手,而且感到PHP太方便太轻松.但在变量作用域这方面却与C有不同的地方,搞不好会相当郁闷,就找不到错误所在.昨晚就与到这么一个问题,是全局变量在函数中的问题.今天搜 ...
- effective OC2.0 52阅读笔记(六 块与大中枢派发)
派发队列:dispatch_queue 操作队列:NSOperationQueue 组:dispathc_group_t 37 理解“块”这一概念 总结:块就是一个值,且自有其相关类型.块的强大之处 ...
随机推荐
- MySQL for Windows 解压缩版配置安装
1.MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装在C: ...
- cssText
cssText 的本质就是设置 HTML 元素的 style 属性值 cssText 的方便之处在于一次可以写很多属性,而且变更 CSS 样式不必变 JS 代码,只需变样式字符串.但它有个缺点,就是它 ...
- [maven] settings 文件节点配置详解
基本结构 <settings xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3. ...
- css--block formatting context
block formatting context(块级格式化上下文) 如何产生BFC:当一个HTML元素满足下面条件的任何一点,都可以产生Block Formatting Context: float ...
- fork&exec
进程是系统进行资源分配和调度的基本单位,包括代码.数据和PCB进程控制块等资源. fork函数通过系统调用创建一个与原进程相同的子进程. 在调用进程(父进程)中返回一次,返回子进程ID:在子进程返回0 ...
- 231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂
231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...
- Mysql执行Update操作时会锁住表
update tableA a,(select a.netbar_id,sum(a.reward_amt) reward_amt from tableB a group by a.netbar_id) ...
- BZOJ4007 [JLOI2015]战争调度
根本想不出来... 原来还是暴力出奇迹啊QAQ 无限ymymym中 /************************************************************** Pr ...
- javascript 把字符串转换为对象
function strToJson(str) { var json = (new Function("return " + str))(); return json;}
- Linux 远程桌面控制
我现在知道有两种方式: 1.直接使用Gnome桌面的远程控制功能.在服务器端登录到gnome桌面,然后在系统菜单中打开远程桌面配置,勾选允许远程即可.这种方式客户端和服务器的两种操作将保持同步,也就是 ...