What variables are destroyed when calling exit(), based on Microsoft’s docs?

I was trying to figure out the differences between terminate(), exit() and abort(), and Microsoft’s
C++ program termination documentation came up in my Google search that compares exit vs abort vs atexit.

I read the first part that compares exit and abort, and it makes sense:

The difference between exit and abort is that exit allows the C++
runtime termination processing to take place (global object destructors get called). abort terminates the program immediately. The abort function bypasses the normal destruction process for initialized global static objects. It also bypasses any special processing that was specified using the atexit function.

Then later, there is a comparison of return and exit with this sample code and corresponding explanation:

// return_statement.cpp
#include <stdlib.h>

struct S 
{
    int value;
};

int main()
{
    S s{ 3 };

    exit( 3 );
    // or
    return 3;
}

The exit and return statements in the preceding example have similar behavior. Both terminate the program and return a value of 3 to the operating system. The difference is that exit doesn’t destroy the automatic variable s, while the return statement does.

  1. I thought that s would also be destroyed when exit() is called. Did I misunderstand, or is there a mistake?

  2. If s isn’t destroyed, how do we reclaim this memory from the stack? What if s were on the heap, would we have leaked this memory, too?

I am not looking for Microsoft-specific solutions. I just found the source and realized that Microsoft does have good documentation for a beginner like me.

5

Global objects are cleanly destructed, but local objects are not. For example, this code:

#include <stdio.h>
#include <stdlib.h>

class Foo {
    char const *s;
public:
    Foo(char const *s) : s(s) {}
    ~Foo() { fprintf(stderr, "cleaning up %s objectn", s); }
};

Foo f("global");

int main() {
    Foo g("local");
    exit(0);
}

Produces this output:

cleaning up global object

Live on Godbolt

For any who might wonder about why this is written the way it is/how it works:

  • Used fprintf instead of std::cerr because when f is being destroyed, we don’t know whether std::cerr still exists or not.
  • All string literals have static storage duration, so when f and g are destroyed, the string literals they attempt to print do both still exist (even though it might sort of seem like the one used by g might be destroyed upon exit from main).

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