C++ should define all local variable outside the loop?
see the following two examples, the conclusion is that we should define variable in the loop if it can.
//test1.cc outside the loop
#include<stdio.h>
int main()
{
int tmp = 0;
for (int i = 0; i < 64; ++i) {
tmp = i;
}
return 0;
}
//test2.cc inside the loop
#include<stdio.h>
int main()
{
for (int i = 0; i < 64; ++i) {
int tmp = i;
}
return 0;
}
The assembly code:
;test1.s
.file "test1.cc"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, -4(%rbp)
movl $0, -8(%rbp)
.L3:
cmpl $63, -8(%rbp)
jg .L2
movl -8(%rbp), %eax
movl %eax, -4(%rbp)
addl $1, -8(%rbp)
jmp .L3
.L2:
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005"
.section .note.GNU-stack,"",@progbits
;test2.s
.file "test2.cc"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, -8(%rbp)
.L3:
cmpl $63, -8(%rbp)
jg .L2
movl -8(%rbp), %eax
movl %eax, -4(%rbp)
addl $1, -8(%rbp)
jmp .L3
.L2:
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005"
.section .note.GNU-stack,"",@progbits
difference of assembly code:
$ diff test1.s test2.s
1c1
< .file "test1.cc"
---
> .file "test2.cc"
13d12
< movl $0, -4(%rbp)
C++ should define all local variable outside the loop?的更多相关文章
- local variable 'xxx' referenced before assignment
这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...
- static local variable
Putting the keyword static in front of a local variable declaration creates a special type of variab ...
- jsp的<%@ include file="jsp/common.jsp" %>报错误Duplicate local variable basePath
将公共引入的文件放到common.jsp中,其他页面引入该jsp即可使用 <%@ page language="java" import="java.util.*& ...
- 【转】The final local variable xxx cannot be assigned, since it is defined in an enclosing type
文地址:http://blog.163.com/benben_long/blog/static/199458243201481102257544/ 本文就自己编程时候遇到的一个问题,简要描述一下,并提 ...
- 【转】Please define the NDK_PROJECT_PATH variable to point to it
原文网址:http://blog.csdn.net/yuanjingjiang/article/details/34857623 Please define the NDK_PROJECT_PATH ...
- Global & Local Variable in Python
Following code explain how 'global' works in the distinction of global variable and local variable. ...
- 遇到local variable 'e' referenced before assignment这样的问题应该如何解决
问题:程序报错:local variable 'e' referenced before assignment 解决:遇到这样的问题,说明你在声明变量e之前就已经对其进行了调用,定位到错误的地方,对变 ...
- RDO Stack Exception: UnboundLocalError: local variable 'logFile' referenced before assignment
Issue: When you install RDO stack on CentOS, you may encounter following error. Error: [root@localho ...
- JSP页面使用include指令出现 Duplicate local variable basePath
现有三个页面 " include.jsp " " a.jsp " " b.jsp " 页面代码如下 首先是a.jsp <%@ page ...
随机推荐
- 【seo】title / robots / description / canonical
1.title title,就是浏览器上显示的那些内容,不仅用户能看到,也能被搜索引擎检索到(搜索引擎在抓取网页时,最先读取的就是网页标题,所以title是否正确设置极其重要. 1)title一般不超 ...
- css常用代码大全以及css兼容(转载)
css常见的快捷开发代码汇总(长期更新),包括CSS3代码,有一些css效果很顽固,经常会一时找不出解决方案,网络上也有很多的工具和高手提供了具体的浏览器兼容代码,这个页面我今后会经常整理,希望能够帮 ...
- python调用函数实现数据的增删改查(2)
1 添加数据 def add(): # 输入姓名,年龄,电话 name=raw_input('name:') age=raw_input('age:') phone=raw_input('phone ...
- JS 马托货物
大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,该如何调配? <!DOCTYPE html> <html> <head> < ...
- 树(2)-----leetcode(层、深度、节点)
1.树的类实现: class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = N ...
- 实验二:1、输出“Hello Word!”;2、测试主方法 的输入参数。3、总结
一.输出:“Hello Word!” 1.新建java项目:点击File->New->Java Project.在project name一栏中输入自己所要创建的项目名称,点击Finish ...
- 域名系统(DNS)
DNS (domain name server/system) 1.基本信息 网络中数据通信依赖ip地址 测试:手动将dns服务地址改为空值,通过ip和域名分别测试网络的联通性 FQDN 完全域名(完 ...
- C# 低耦合 高内聚
低耦合 loosely Coupling 松散的耦合关系=炮友 couple=夫妻 夫妻=法律约束.家庭.生活.财产.繁衍 炮友:吃喝玩乐,不会产生感情方面的依赖 内聚性 内聚性又称块内联 ...
- static final常量变量的正确书写规范
AccountConstants.java类 命名:常量类以Constants单词命名结尾 package com.paic.pacz.core.salesmanage.util; import ja ...
- ZooKeeper分布式集群部署及问题
ZooKeeper为分布式应用系统提供了高性能服务,在许多常见的集群服务中被广泛使用,最常见的当属HBase集群了,其他的还有Solr集群.Hadoop-2中的HA自己主动故障转移等. 本文主要介绍了 ...