当同一个类的两个对象在同一个成员变量上运行时,堆栈帧是什么? [英] What is the stack frame when two objects of the same class operate on the same member variable?

查看:84
本文介绍了当同一个类的两个对象在同一个成员变量上运行时,堆栈帧是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下示例代码有几个问题:



I have a few questions on the example code below :

#include <iostream>

using namespace std;

class Example
{
    int a;
    public:
    void setval(int x);
 
    int addval(Example &ex);
};

void Example::setval(int x)
{
    a=x;
}


int Example::addval(Example &ex)
{
    int c;
    c=ex.a+a;
    return c;
}

int main()
{
    Example obj1,obj2;
    int sum;
    obj1.setval(10);
    obj2.setval(20);
    sum=obj1.addval(obj2);
    cout<<" the sum is :"<<sum<<endl;
}




Output :
$g++ -o main *.cpp
$main
 the sum is :30



问题:

1>当行sum = obj1.addval(obj2);执行,函数堆栈是什么样的?

1.a>在通过obj1调用addval(obj2)时,obj1.addval的堆栈是否包含变量obj2.a的内存地址?



2>变量c的范围是多少?



3> ca成员是变量吗?



我尝试过:



如果我无法澄清我的问题,请说出来。

预先感谢您的回复。

问候!


Questions:
1> When the line sum=obj1.addval(obj2); executes, what does the function stack look like?
1.a> in the call to addval(obj2) by obj1, does the stack of obj1.addval contain the memory address of the variable obj2.a ?

2> What is the scope of the variable c ?

3> Is c a member variable ?

What I have tried:

If I have not been able to make my question clear, please say so.
Thanks in advance for your responses.
Regards!

推荐答案

g ++ -o main * .cpp
g++ -o main *.cpp


main
总和是:30
main the sum is :30



问题:

1>当行sum = obj1.addval(obj2);执行,函数堆栈是什么样的?

1.a>在通过obj1调用addval(obj2)时,obj1.addval的堆栈是否包含变量obj2.a的内存地址?



2>变量c的范围是多少?



3> ca成员是变量吗?



我尝试过:



如果我无法澄清我的问题,请说出来。

提前感谢您的回复。

问候!


Questions:
1> When the line sum=obj1.addval(obj2); executes, what does the function stack look like?
1.a> in the call to addval(obj2) by obj1, does the stack of obj1.addval contain the memory address of the variable obj2.a ?

2> What is the scope of the variable c ?

3> Is c a member variable ?

What I have tried:

If I have not been able to make my question clear, please say so.
Thanks in advance for your responses.
Regards!


首先,我建议您彻底学习编译器的调试器。通过单步执行代码并查看CPU寄存器和原始内存显示的内容,您可以了解相关内容。如果您没有,Visual Studio 2017可在Community Edition中免费使用。我有它并且使用它非常好。



1.我不确定你的意思是功能堆栈。处理器有一个堆栈,当调用函数时,它们会在其上推送返回地址,并在从函数返回时弹出它们。从返回地址开始,调试器可以识别调用堆栈,该调用堆栈是哪个函数调用的列表。请记住,当调用函数时,编译器将知道哪些CPU寄存器将受到影响,并在调用时将它们推送到堆栈中并将其弹出并在返回时恢复它们。您可以通过查看可由编译器生成的汇编语言列表来查看执行此操作的说明。这可能很有启发性。



调用addval时,编译器会将调用Example对象的this指针和引用ex推送到堆栈上。它还会推送调用者的返回地址和将受影响的任何寄存器的内容。



1a。不会。推动obj2的这个指针,它的'a'成员将是一个偏移量。



2. c是addval方法的本地。当从addval返回执行时,c超出范围,因此它将被销毁。对于一个除了对象之外什么都不做的整数,会调用它们的析构函数来释放任何分配的资源。



3.不,c是addval方法的本地。



至于你的另一个问题,不,对象没有自己的堆栈。在堆栈或堆上创建对象时,会为其数据成员分配内存。函数也没有完全拥有自己的堆栈。它们具有所谓的堆栈帧,它基本上是一个基址寄存器,它保存一个地址(调用函数时的堆栈指针),局部变量是它的偏移量。同样,您应该查看一些汇编列表以了解其工作原理。您将看到传递参数的地址如何从堆栈基址向一个方向偏移,局部变量在另一个方向偏移。
Firstly, I recommend learning your compiler's debugger thoroughly. You can learn quite a bit about this stuff by single-stepping through some code and looking at the contents of the CPU's registers and raw memory displays. If you don't have one, Visual Studio 2017 is available for free in the Community Edition. I have it and use it and it is very good.

1. I'm not sure what you mean by "function stack". The processor has a stack and return addresses are pushed on it when functions are called and they are popped when returning from the function. From the return address, the debugger can discern a call stack which is a list of which function has called which. Keep in mind that when functions are called the compiler will know which CPU registers will be affected and it pushes those on the stack at call time and pops them off and restores them at return. You can see the instructions that do this by looking at assembly language listings that can be generated by the compiler. That can be instructive.

When calling addval, the compiler will push the this pointer of the calling Example object and the reference ex on the stack. It also pushes the return address of the caller and the contents of any registers that will be affected.

1a. No. The this pointer of obj2 is pushed and its 'a' member will be an offset from that.

2. c is local to the addval method. When execution has returned from addval c is out of scope so it will be destroyed. For an integer that does nothing but objects will have their destructors called to release any resources allocated.

3. no, c is local to the addval method.

As to your other question, no, objects do not have their own stack. When an object is created, either on the stack or the heap, memory is allocated for its data members. Functions don't exactly have their own stack either. They have what is called a stack frame which is basically a base register which holds an address (the stack pointer when the function was called) and local variables are offsets from it. Again, you should look at some assembly listings to see how this works. You will see how the addresses of passed arguments are offset one direction from the stack base and local variables are offset in the other direction.


这篇关于当同一个类的两个对象在同一个成员变量上运行时,堆栈帧是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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