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?的更多相关文章

  1. local variable 'xxx' referenced before assignment

    这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...

  2. static local variable

    Putting the keyword static in front of a local variable declaration creates a special type of variab ...

  3. jsp的<%@ include file="jsp/common.jsp" %>报错误Duplicate local variable basePath

    将公共引入的文件放到common.jsp中,其他页面引入该jsp即可使用 <%@ page language="java" import="java.util.*& ...

  4. 【转】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/ 本文就自己编程时候遇到的一个问题,简要描述一下,并提 ...

  5. 【转】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 ...

  6. Global & Local Variable in Python

    Following code explain how 'global' works in the distinction of global variable and local variable. ...

  7. 遇到local variable 'e' referenced before assignment这样的问题应该如何解决

    问题:程序报错:local variable 'e' referenced before assignment 解决:遇到这样的问题,说明你在声明变量e之前就已经对其进行了调用,定位到错误的地方,对变 ...

  8. 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 ...

  9. JSP页面使用include指令出现 Duplicate local variable basePath

    现有三个页面 " include.jsp " " a.jsp " " b.jsp " 页面代码如下 首先是a.jsp <%@ page ...

随机推荐

  1. Language Integrated Query

    Language Integrated Query (LINQ, pronounced "link") is a Microsoft .NET Framework componen ...

  2. day01_20181223

    今日内容大纲:1,python的出生于应用.2,Python的历史.    python2x:        源码冗余,源码重复,源码不规范.    python3x:源码清晰优美简单.       ...

  3. 初步使用vue中axios

    1.下载axios npm install axios --save 2.两种方式使用axios (1)在模块中引入axios 例如:我在用户登陆界面需要使用axios,就在login页面引入,不是全 ...

  4. lsync 负载实现代码双向同步

    参考文件: https://www.zyku.net/centos/1713.html 检查错误命令: lsyncd -pidfile /tmp/lsyncd.pid  /etc/lsyncd.con ...

  5. 数据库之JDBC入门

    数据表: 代码实现(注:jar包用的8.0版本) import java.sql.*; import java.util.Scanner; public class MyDatabase { publ ...

  6. Netty、NIO、多线程

    一:Netty.NIO.多线程? 时隔很久终于又更新了!之前一直迟迟未动也是因为积累不够,后面比较难下手.过年期间@李林锋hw发布了一个Netty5.0架构剖析和源码解读,看完也是收获不少.前面的文章 ...

  7. 转[总结]FFMPEG视音频编解码零基础学习方法 .

    http://blog.csdn.net/leixiaohua1020/article/details/15811977 在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视 ...

  8. Problem 6

    Problem 6 # Problem_6.py """ The sum of the squares of the first ten natural numbers ...

  9. 性能测试之Jforum平台的搭建

    学习Jmeter性能基础,想要借助1款现有的软件平台,来练习jmeter基础,<Jmeter实战>书籍上给出样例软件平台:Jforum 一.环境准备 准备:tomcat9.mysql5.5 ...

  10. Mysql和网络连接相关的参数介绍

    摘要:同时还需要注意OS级别的网络监听队列的限制.因为如果OS的网络监听设置小于mysql的back_log设置的时候,我们加大back_log是没有意义的... max_conecctions:整个 ...