-3

In my C++ program, I'm working with destructors and constructors in classes. For no apparent reason, when I initiate the constructor, the destructor is also run. This is the function code:

#include "Foo.h"

int main()
{
    Foo f;
    return 0;
}

The normal output should be just "this is the ctor", but for some reason it prints both the constructor output and the destructor output ("this is the ctor this is the dtor"). There is no reason for it to run the destructor because I didn't delete the object.

If anyone has any idea why this is happening, I'd be happy to know.

This is the class code, if needed:

#pragma once

class Foo
{
public:
    Foo();
    Foo(int bok);
    ~Foo();     
};

I am just trying to test default constructors, overloaded constructors, and destructors.

It should usualy print out the constructor as stated here:

Foo::Foo()
{
    std::cout << "this is the  default  ctor" << std::endl;
}
 
Foo::~Foo()
{
    std::cout << "this is  the dtor" << std::endl; 
    // and this is the dtor output
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    The destructor runs when the object goes out of scope, at the end of the function where it is declared. – BoP Feb 20 '23 at 22:48
  • The instance `f` is an automatic variable. It'll get automatically destroyed (its destructor will be called) at the end of the scope. That is, when the program returns from `main`. – Ted Lyngmo Feb 20 '23 at 22:48
  • @TedLyngmo is there a way for the dtor not to get called at the end of main, for example something that would stop the deconstructor from being called in main specificly? – IcantFind_A_Name Feb 20 '23 at 22:55
  • @IcantFind_A_Name Sure. You can allocate it dynamically: `auto f = new Foo;` - and in this case, it'd leak when you return from `main` – Ted Lyngmo Feb 20 '23 at 22:58
  • @CaptainGiraffe oh thank you, i didnt think the object would get destoryed because of return – IcantFind_A_Name Feb 20 '23 at 23:00
  • @CaptainGiraffe Sure it's a temporary leak but a leak nevertheless. If moved into the deep of a program it may be hard to find such a leak – Ted Lyngmo Feb 20 '23 at 23:01
  • @IcantFind_A_Name It's not `return` itself that causes it to get destroyed. It's leaving the scope that does it. You can remove `return 0;` and it'll still get destroyed. – Ted Lyngmo Feb 20 '23 at 23:02
  • @CaptainGiraffe :-) I wasn't nitpicking your comment. I used _"returns"_ myself up there. I just wanted to clarify it to OP since _"i didnt think the object would get destoryed because of return"_ sounded like the _"return"_ part was misunderstood. – Ted Lyngmo Feb 20 '23 at 23:05
  • @TedLyngmo ok thank you i will use that – IcantFind_A_Name Feb 20 '23 at 23:06
  • 1
    @IcantFind_A_Name _"i will use"_ ... what? `new` ? You probably shouldn't. I just answered your question that it's possible to not have it automatically destroyed at the end of the scope. It's usually not a good idea to create raw owning pointers though. – Ted Lyngmo Feb 20 '23 at 23:07
  • `Foo f` in your code defines `f` as an object with automatic storage duration. Such an object ceases to exist (its destructor is called) at the end of the enclosing scope. In your case, that means it is destroyed when `main()` returns. There is no need to `delete` the object (in fact, doing so would cause undefined behaviour, since a `delete` expression can only be used (in the sense of not causing undefined behaviour) on a pointer produced by a corresponding `new` expression). – Peter Feb 21 '23 at 00:08
  • The tag [tag:deconstructor] is wrong here. The `~Foo()` is called [tag:destructor], not deconstructor. – heap underrun Feb 21 '23 at 02:11
  • 1
    @IcantFind_A_Name *Their is no reason for it to run the deconsructor because i didnt deleat the object,* -- That is not how C++ works. You create an automatic object, once it goes out of scope, the destructor is automatically called. It is a **good** thing that C++ works this way. This allows us to write objects that will clean up themselves *automatically*, without having the programmer calling `delete` all over the place. – PaulMcKenzie Feb 21 '23 at 05:26

1 Answers1

1

Any code that looks like

 {
    Foo bar;
 } // Destruction here

Has a construction at the declaration site and a destruction when bar reaches it's end of scope. As Ted kindly points out this is called an automatic. That's what you are seeing.

For automatic objects, the lifetime, construction + destruction is identical to it's scope. https://en.cppreference.com/w/cpp/language/scope

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67