关于变量的重新声明 [英] About redeclaration of a variable

查看:102
本文介绍了关于变量的重新声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将变量声明为int x并编译了程序,当我从程序中删除此变量,并且在编译后我再次重新声明了与int x相同的变量,那么该变量的地址将在内存中相同在重新声明期间?并且地址是否更改,为什么?

I have declared a variable as int x and i compiled the program and when i delete this variable from the program and after compilation i again redeclare the same variable as int x, then will the address be same for this variable in the memory during redeclaration?and if the address changes ,why is it?

推荐答案

链接程序根据在符号表中找到地址的顺序分配地址或偏移量.
它与变量的名称无关.
因此,使用相同的名称重新声明变量并不能保证以前分配的地址相同.
A linker allocates addresses or offsets based on the order it finds it in the symbol table.
It has got nothing to do with the name of the variable.
So using the same name to re-declare a variable does not guarantee the same address that was previously allocated.


// Stack address: 0000100
for(int i=0;i<10;i++){ /*something */ } // Stack address: 00000FC

// Stack address: 0000100
// do anything else

for(int i=0;i<10;i++){ /*something */ } // Stack address: 00000FC

// Stack address: 0000100


您可以看到-该程序在堆栈上重用了相同的地址.
或以另一种方式


You can see - the program reuses the same address on the stack.
Or in another way

void function(int x,int y)
{
  if(1==x)
  {
    int  a;
    // do...
  }
  if(1==y)
  {
    int  z; // <- ''z'' uses the same adress as ''a'' before
  }
}


问候.


这篇关于关于变量的重新声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆