Alex4
Alex4

Reputation: 11

Why my compiler does not work as expected?

I tried this code on multiply online compilers and it work well but when I try it on my there are problems(well not exactly errors, but it not working as expected).

The destructor is not called for both static and gloabl objects. And if I put function create behind main then after it call constructor for objects in create, it does not doing anything afterwards, does not calling any destructor and does not even print message that are after function create.

I'm using Microsoft Visual Studio Community 2017 version 15.9.17 if it is of any use.

So is there any solution to these or I need to install Visual Studio all over again?

// Expected

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)  

// Acctual

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)  
  

Acctual if I put function create behind 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 ```

Upvotes: 1

Views: 196

Answers (2)

Barrnet Chou
Barrnet Chou

Reputation: 1923

The global object is constructed before the main function is executed, and the local static object is constructed before the execution of the function body. The local static object is destructed earlier than the global object. They are all after the main function.

I tested the code and it worked fine. But I found this problem under command line compilation. So this should be because the console disappeared before it was displayed. I suggest that you could add the following code.

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 ``

If it still doesn't work, I suggest that you could using Debug to trace the code.

Upvotes: 0

zkoza
zkoza

Reputation: 2860

Check in the debugger (or by saving some logs on the disk instead of using std::cout) if the destructors are actually not called. Perhaps this is "only" std::cout for which the destructor is called before the destructors of the global and static objects defined in your compilation unit.

See: Using cout in destructors of static objects

Upvotes: 1

Related Questions