I have a question based on SmallTalk and C++. In one of the programming languages books, there is a question like:
“Does an object in Smalltalk require its own private stack? What about in C++?”
Could you please explain those questions above if you know the answers?
Thanks.
1
In no language does each object require its own stack. Not in Smalltalk. Not in C++. Not in any other language in common use.
The only notable exception to this would be thread objects, or objects that otherwise embody concurrency context. Each thread of execution requires its own call stack. That is true independent of the language used–Smalltalk, C++, Python, or whathaveyou.
NB Some languages or environments attempt to achieve concurrency (the effect of “multi-threading”) without per-thread stacks in order to reduce overhead. Such green threads or “greenlets” are quite useful and increasingly common. Concurrency in go, for example, is centered around this idea. But even if green threads are called “threads,” they behave closer to coroutines than true threads. Those that are “stackless” are simulating multi-threaded concurrency, not implementing it fully.
I know nothing about smalltalk, so I’ll only talk C++.
In C++ (and AFAIK in Java and C#), each thread has its own stack. If you don’t know what a thread is, I suggest you read about it. I’ll describe it here as the bus your code is taking a ride on. But I suggest you learn more than this horrible analogy (Also, not all C runtimes are multithreaded, so there may be only one thread. But you probably won’t deal with those).
If you declare something inside a function like so:
void foo() {
MyObject a;
}
Then your new MyObject instance lives on the stack. If you have 2 objects:
void foo() {
MyObject a;
MyOtherObject b;
}
Both of them live on the stack (the same stack). If you use new/malloc/HeapAlloc/whatever other heap allocation routine your platform has:
void foo() {
MyObject* a = new MyObject();
}
Then your new object instance is allocated in the heap, which is a different thing, which I won’t describe since it’s probably out of scope.
In short, the answer is no. Each object does not have its own stack.
1
I cannot speak for Smalltalk, but for C++, the stack is used by the entire program. Each object in the program shares the stack, based on when they are used. Objects in the stack are managed by the OS and are deleted when they fall “out of scope” in C++. For example, consider the following code:
// Global declarations and function prototypes
int x, y;
void someFunction();
int main()
{
someFunction();
// w and x are deleted here
}
void someFunction()
{
int w, x;
}
The variables x
and y
will remain on the stack until the program closes because they are global. The variables w
and x
will be declared and thus “grow” the stack when the program enters someFunction()
so they can be used, and then will be deleted and thus “shrink” the stack when someFunction()
exits.
Even objects such as classes do not have their own stack, because they are ultimately using the stack when they are instantiated, and are destroyed when they fall out of scope via the destructor.
A better way to say it is that each program gets its own stack, but the objects in it do not.
2