变量如何存储在堆栈上?

佐伊卜·阿斯兰(Zohaib Aslam)

我读过有两个堆栈的内存区域,一个是堆栈,另一个是堆。基本数据类型(如int,double,float等)存储在堆栈中,而引用类型存储在堆中。众所周知,堆栈是LIFO指最后压入的元素将被首先移除。现在假设以下代码

int first = 10;
double second = 20.0;
float third = 3.0F;

因此,first将先被推,然后second被推third因此third,类型为float的变量将位于堆栈的顶部,但是如果我使用以下代码(假设在C#中)

Console.WriteLine(second);

second当变量third位于堆栈顶部时,如何访问变量的值?

NoMadCap

堆栈的行为与LIFO相同,具有PUSH和POP指令。但这并不意味着没有pop即可读取堆栈内存。就你而言

        push int first            (* its not a opcode of machine, just trying to explain)
        push  double second
        push float third 

        Now you have 2 options to access the variables that you have pushed.

       1) pop -> This is the one that reads and makes stack look like lifo.
         if you pop it
             stack will be
                    int first
                    double second.
            Bsically it removes(not exactly,just a register is chaged to show the stacks last valid memory position)

      2) But if you want you can jst read it without pop.Thus not removing the last times.
         So you will say Read me  double.And it will access the same way it does in heaps..
                  That will cause machine to execute  a mov instruction .

             Please note its EBP(Base pointer) and ESP(Stack pointer) that points to the location of a stacks variables.And machines read variables   as  mov eax,[ebp+2(distance of "second" from where base pointer is now pointing]].

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

变量如何存储在堆栈上?

来自分类Dev

堆栈存储器如何工作或如何在堆栈上分配和访问函数变量

来自分类Dev

局部变量如何存储在堆栈中?

来自分类Dev

如何演示ac#变量的存储,堆栈或堆在哪里

来自分类Dev

程序集如何在堆栈上访问/存储变量

来自分类Dev

清除堆栈上的变量

来自分类Dev

清除堆栈上的变量

来自分类Dev

如何正确引用堆栈上的局部变量

来自分类Dev

如何实现堆栈上堆栈?

来自分类Dev

变量在堆栈上是否连续?

来自分类Dev

堆栈上的局部变量

来自分类Dev

数组如何存储在堆栈中?

来自分类Dev

数组如何存储在堆栈中?

来自分类Dev

推送目录堆栈如何存储?

来自分类Dev

C堆栈变量是否反向存储?

来自分类Dev

指向存储在堆栈中的变量的指针

来自分类Dev

如何在堆栈上存储函数字符串参数

来自分类Dev

如何为非默认构造变量在堆栈上保留空间?

来自分类Dev

如何确定堆栈上的内容?

来自分类Dev

堆栈存储器如何组织?

来自分类Dev

如何在onSaveInstanceState()中存储堆栈?

来自分类Dev

如何在onSaveInstanceState()中存储堆栈?

来自分类Dev

如何在SharedPreferences中存储堆栈

来自分类Dev

在变量上存储几个元素

来自分类Dev

在变量上存储$ _SESSION ['user']

来自分类Dev

方法中的本地最终变量存储在哪里(堆栈/堆)?

来自分类Dev

函数的变量存储在哪里?在堆栈还是堆上?

来自分类Dev

闭包中的变量存储在哪里-堆栈还是堆?

来自分类Dev

如何在堆栈或堆上定位变量