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.
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 理解“块”这一概念 总结:块就是一个值,且自有其相关类型.块的强大之处 ...
随机推荐
- SAP销售订单状态修改(审核) 计划行自动产生需求,产生MD04需求
不知道业务怎么配置的,创建销售单时,一堆计划行类别,什么CN,DN...都有,但是审核后需要计划行变更为CP,这样在MD04才能看到需求. 原有逻辑是弄个后台程序,审核后调一下,更新一下计划行,这样是 ...
- jQuery_pager.js分页
在做前端项目中,总是需要自己手写类似于这样的分页效果: 这就需要使用jQuery.pager.js文件,其使用方法为:在html中引入三个文件,分别为: <link rel="styl ...
- TokuDB介绍——本质是分形树(一个叶子4MB)+缓存减少写操作
其性能特点见:http://www.cnblogs.com/billyxp/p/3567421.html TokuDB 是一个高性能.支持事务处理的 MySQL 和 MariaDB 的存储引擎.Tok ...
- Ubuntu 设置su密码
如果之前安装时没有设置root密码,可以如下设置: 命令窗口中输入:sudo passwd [sudo] password for 用户名: 这里输入你sudo 的密码输入新的 UNIX 密码: 重 ...
- Eclipse导出可执行Jar文件(包含第三方Jar包)
1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...
- layer.js定制弹窗
<button>点击</button> <div class="order" id="order"> 定制 </div ...
- MySQL事务隔离级别详解
原文地址:http://xm-king.iteye.com/blog/770721 SQL标准定义了4类隔离级别,包括了一些具体规则,用来限定事务内外的哪些改变是可见的,哪些是不可见的.低级别的隔离级 ...
- swift 开眼今日精选
swift 开眼今日精选 import UIKit class TodayController: UITableViewController { vararray =NSMutableArray() ...
- 提升WordPress站点速度的八个建议
WordPress是一个很棒的开源程序,几乎我认识的站长朋友当中,粗略估算有80%使用Wordpress.但很棒不等于完美,就在我所认识的这些朋友中,几乎所有人都会抱怨Wordpress太臃肿,运行效 ...
- POJ 1328 Radar Installation 贪心 难度:1
http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆 ...