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. In-Out Parameters inout keyword

    You write an in-out parameter by placing the inout keyword right before a parameter’s type. An in-ou ...

  2. VS Code中编写html(4) 标签的宽高颜色背景设置

    1  <!+Tab键--> <!--有两个div标签时,分别设置style,有两种方法--> <div id="div1">第一个div标签:& ...

  3. python django整理(五)配置favicon.ico,解决警告Not Found: /favicon.ico(转载)

    版权声明:本文为博主原创文章,欢迎交流分享,未经博主允许不得转载. https://blog.csdn.net/HHTNAN/article/details/78549561 Django 浏览器打开 ...

  4. Day 08 字符编码

    字符编码 计算机基础 启动应用程序 1.双击QQ 2.操作系统接受指定然后把该操作转化为0和1发送给CPU 3.CPU接受指令然后把指令发给内存 4.内存接受指令把指令发送给硬盘获取数据 5.QQ在内 ...

  5. LCIS 最长公共上升子序列问题DP算法及优化

    一. 知识简介 学习 LCIS 的预备知识: 动态规划基本思想, LCS, LIS 经典问题:给出有 n 个元素的数组 a[] , m 个元素的数组 b[] ,求出它们的最长上升公共子序列的长度. 例 ...

  6. Project Euler 21 Distinct primes factors( 整数因子和 )

    题意: 记d(n)为n的所有真因数(小于n且整除n的正整数)之和. 如果d(a) = b且d(b) = a,且a ≠ b,那么a和b构成一个亲和数对,a和b被称为亲和数. 例如,220的真因数包括1. ...

  7. locate-updatedb命令检索不全

    locate-updatedb命令检索不全 执行updatedb命令,用于立刻更新locate命令所必需的数据库文件,但有些文件可能会在检索过程中被过滤掉. 有时候明明存在的文件,用find命令都能搜 ...

  8. solrj 操作 solr 单机版

    一.导入 jar 包 <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr- ...

  9. rabbitMQ学习笔记(二) 简单的发送与接收消息 HelloWorld

    首先要下载rabbitmq的javaClient库,然后加入到项目中,下载地址为:http://www.rabbitmq.com/releases/rabbitmq-java-client/v3.1. ...

  10. C专家编程之为什么C语言把数组形參当做指针:数组/指针实參

    #include<stdio.h> void print_array_test(char ca[]) { printf("ca : %s\n",ca); printf( ...