Click here to Skip to main content
15,749,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a few questions on the example code below :

#include <iostream>

using namespace std;

class Example
    int a;
    void setval(int x);
    int addval(Example &ex);

void Example::setval(int x)

int Example::addval(Example &ex)
    int c;
    return c;

int main()
    Example obj1,obj2;
    int sum;
    cout<<" the sum is :"<<sum<<endl;

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

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.
Updated 2-Oct-18 10:42am
Gerry Schmitz 27-Sep-18 17:28pm    
These aren't "normal" questions for a "beginner". What's your point? Is this homework?
out0FBox 27-Sep-18 20:05pm    
Just a C programmer wading into OOP. Interested in the internal workings.
out0FBox 27-Sep-18 20:15pm    
I am assuming objects have a stack of their own where they have a copy of the members of a class, much like functions have a stack of their own containing local variables.
If objects are stored in a heap, i will have to reconsider the question. Any suggestions will help.

1 solution

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.
Share this answer
out0FBox 2-Oct-18 16:36pm    
Thanks a ton for the explanation Rick. I will try your approach as i debug.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900