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 ...
随机推荐
- (转) shiro权限框架详解04-shiro认证
http://blog.csdn.net/facekbook/article/details/54906635 shiro认证 本文介绍shiro的认证功能 认证流程 入门程序(用户登录和退出) 自定 ...
- JavaScript回顾一下js的基础知识,以及学习一下在项目中了解到的新知识
学习文档:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Functions https://www.cnblogs.com ...
- mvvm模式和mvc模式 概述总结对比
1.mvc模式简介: MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范.例如: angular ...
- Vue2:实例生命周期、js生命周期
1.vue2生命周期 每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如,实例需要配置数据观测(data observer).编译模版.挂载实例到 DOM ,然后在数据变化时更新 DOM ...
- CodeForces-546D Soldier and Number Game 筛法+动态规划
题目链接:https://cn.vjudge.net/problem/CodeForces-546D 题意 抱歉,我给忘了,现在看题目又看不懂: P 思路 筛法+dp 话说这个函数应该是积性函数,然后 ...
- VS2015使用C++编写DLL,并在C#环境中调用【转】
说明:本文是本人在学习封装DLL时看到的,觉得说得很清楚,为了防止忘记,特意复制下来学习用,原文链接:https://blog.csdn.net/songyi160/article/details/5 ...
- 【codeforces 805A】Fake NP
[题目链接]:http://codeforces.com/contest/805/problem/A [题意] 问你在l..r这个区间内的所有数字: 对于每个数的因子; 出现次数最多的是哪一个; [题 ...
- RabbitMQ学习总结(7)——Spring整合RabbitMQ实例
1.RabbitMQ简介 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. 官网:http://www.rabbitmq. ...
- C#中的Socket
];//用于缓存客户端所发送的信息,通过socket传递的信息必须为字节数组 IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);//本机预使用 ...
- T4系列文章之1:认识T4
一.导读 MSDN:Code Generation and T4 Text Templates 博客园:编写T4模板进行代码生成 Oleg Sych系列文章:http://www.olegsych.c ...