Objects in smalltalk and c++ [closed]

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Objects in smalltalk and c++ [closed]

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật