为什么我的编译器没有按预期工作?

时间:2021-02-05 09:00:09

标签: c++ visual-studio compiler-errors visual-studio-2017

我在多个在线编译器上尝试了这段代码,它运行良好,但是当我在我的机器上尝试时出现了问题(不完全是错误,但它没有按预期工作)。

不会为静态和全局对象调用析构函数。如果我将函数 create 放在 main 之后,然后在它为 create 中的对象调用构造函数之后,它之后不会做任何事情,不会调用任何析构函数,甚至不会打印函数创建之后的消息。

我正在使用 Microsoft Visual Studio Community 2017 版本 15.9.17,如果它有任何用处。

那么有没有办法解决这些问题,或者我需要重新安装 Visual Studio?

// 预期

Object 1    constructor runs    (global before main)

MAIN FUNCTION: EXECUTION BEGINS  
Object 2    constructor runs    (local automatic in main)  
Object 3    constructor runs    (local static in main)

CREATE FUNCTIONS: EXECUTION BEGINS   
Object 5    constructor runs    (local automatic in create)             
Object 6    constructor runs    (local static in create)   
Object 7    constructor runs    (local automatic in create)

CREATE FUNCTION: EXECUTION ENDS  
Object 7    destructor runs (local automatic in create)  
Object 5    destructor runs (local automatic in create)

MAIN FUNCTION: EXECUTION RESUMES  
Object 4    constructor runs    (local automatic in main)  

MAIN FUNCTION: EXECUTION ENDS  
Object 4    destructor runs (local automatic in main)  
Object 2    destructor runs (local automatic in main)  
  
Object 6    destructor runs (local static in create)  
Object 3    destructor runs (local static in main)  
  
Object 1    destructor runs (global before main)  

// 实际

Object 1    constructor runs    (global before main)  
  
MAIN FUNCTION: EXECUTION BEGINS  
Object 2    constructor runs    (local automatic in main)  
Object 3    constructor runs    (local static in main)  
  
CREATE FUNCTIONS: EXECUTION BEGINS  
Object 5    constructor runs    (local automatic in create)  
Object 6    constructor runs    (local static in create)  
Object 7    constructor runs    (local automatic in create)  
  
CREATE FUNCTION: EXECUTION ENDS  
Object 7    destructor runs (local automatic in create)  
Object 5    destructor runs (local automatic in create)  
  
MAIN FUNCTION: EXECUTION RESUMES  
Object 4    constructor runs    (local automatic in main)  
  
MAIN FUNCTION: EXECUTION ENDS  
Object 4    destructor runs (local automatic in main)  
Object 2    destructor runs (local automatic in main)  
  

实际如果我把函数 create 放在 main 后面

Object 1    constructor runs    (global before main)  
  
MAIN FUNCTION: EXECUTION BEGINS  
Object 2    constructor runs    (local automatic in main)  
Object 3    constructor runs    (local static in main)  
  
CREATE FUNCTIONS: EXECUTION BEGINS  
Object 5    constructor runs    (local automatic in create)  
Object 6    constructor runs    (local static in create)  
Object 7    constructor runs    (local automatic in create)  
  
CREATE FUNCTION: EXECUTION ENDS  
#include <iostream>
using std::cout;
using std::endl;

class CreateAndDestroy {
public:
    CreateAndDestroy(int, const char*); // constructor
    ~CreateAndDestroy();            // destructor

private:
    int objectID;
    const char* message;
};  // end class CreateAndDestroy

// constructor
CreateAndDestroy::CreateAndDestroy(int objectNumber, const char* messagePtr) {
    objectID = objectNumber;
    message = messagePtr;

    cout << "Object " << objectID << "  constructor runs    " << message << endl;
}   // end CreateAndDestroy constructor

// destructor
CreateAndDestroy::~CreateAndDestroy() {
    cout << (objectID == 1 || objectID == 6 ? "\n" : "");

    cout << "Object " << objectID << "  destructor runs " << message << endl;
}   // end ~CreateAndDestroy destructor

void create(void);  // prototype

// functions to create objects
void create(void) {
    cout << "\nCREATE FUNCTIONS: EXECUTION BEGINS" << endl;
    CreateAndDestroy fifth(5, "(local automatic in create)");
    static CreateAndDestroy sixth(6, "(local static in create)");
    CreateAndDestroy seventh(7, "(local automatic in create)");
    cout << "\nCREATE FUNCTION: EXECUTION ENDS" << endl;
}   // end function create

    // global object
CreateAndDestroy first(1, "(global before main)");

int main(int argc, char *argv) {
    cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl;
    CreateAndDestroy second(2, "(local automatic in main)");
    static CreateAndDestroy third(3, "(local static in main)");
    create();   // call function to create objects
    cout << "\nMAIN FUNCTION: EXECUTION RESUMES" << endl;
    CreateAndDestroy fourth(4, "(local automatic in main)");
    cout << "\nMAIN FUNCTION: EXECUTION ENDS" << endl;

    return 0;
}   // end main ```

2 个答案:

答案 0 :(得分:1)

如果析构函数实际上没有被调用,请检查调试器(或通过在磁盘上保存一些日志而不是使用 std::cout)。也许这是“唯一的”std::cout,在编译单元中定义的全局和静态对象的析构函数之前调用析构函数。

见:Using cout in destructors of static objects

答案 1 :(得分:0)

全局对象在main函数执行前构造,局部静态对象在函数体执行前构造。局部静态对象比全局对象更早被破坏。它们都在 main 函数之后。

我测试了代码,它运行良好。但是我在命令行编译下发现了这个问题。所以这应该是因为控制台在显示之前就消失了。我建议您可以添加以下代码。

int main(int argc, char* argv) {
    cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl;
    CreateAndDestroy second(2, "(local automatic in main)");
    static CreateAndDestroy third(3, "(local static in main)");
    create(); // call function to create objects
    cout << "\nMAIN FUNCTION: EXECUTION RESUMES" << endl;
    CreateAndDestroy fourth(4, "(local automatic in main)");
    cout << "\nMAIN FUNCTION: EXECUTION ENDS" << endl;
    getchar();
    system("pause");
    return 0;
} // end main ``

如果还是不行,建议您可以使用Debug来跟踪代码。

相关问题