POV-Ray : Newsgroups : povray.off-topic : C++ destructor question Server Time
3 Sep 2024 19:20:49 EDT (-0400)
  C++ destructor question (Message 1 to 7 of 7)  
From: Darren New
Subject: C++ destructor question
Date: 5 Aug 2010 15:27:25
Message: <4c5b109d@news.povray.org>
Speaking of RAII and all...

When an object is stuck into a standard collection in C++, and then the 
collection goes out of scope, what order is defined on the destructors of 
the things in the collection? I wouldn't imagine it's the order they were 
inserted (at least for a random-access type collection).  Does a stack 
destruct in a different order than a queue does, for example? Does a vector 
always destruct on increasing indexes?  (An array doesn't call the 
destructors of its element, does it?)

-- 
Darren New, San Diego CA, USA (PST)
    C# - a language whose greatest drawback
    is that its best implementation comes
    from a company that doesn't hate Microsoft.


Post a reply to this message

From: Warp
Subject: Re: C++ destructor question
Date: 5 Aug 2010 16:04:28
Message: <4c5b194c@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> When an object is stuck into a standard collection in C++, and then the 
> collection goes out of scope, what order is defined on the destructors of 
> the things in the collection? I wouldn't imagine it's the order they were 
> inserted (at least for a random-access type collection).  Does a stack 
> destruct in a different order than a queue does, for example? Does a vector 
> always destruct on increasing indexes?  (An array doesn't call the 
> destructors of its element, does it?)

  Funny, I have never thought of that. If I had to guess, I'd say that
the standard leaves it up to the implementation.

  Arrays (either stack-allocated or dynamically allocated with 'new') are,
however, guaranteed to be constructed in the order of increasing indexes
and destroyed in the reverse order.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: C++ destructor question
Date: 5 Aug 2010 16:31:06
Message: <4c5b1f8a$1@news.povray.org>
Warp wrote:
>   Funny, I have never thought of that. 

I can see where having a stack destruct as if it's popping things could be 
useful, for example. The topic came up discussing the Go "defer" bit in 
another thread.

> If I had to guess, I'd say that
> the standard leaves it up to the implementation.

OK. I'll look it up if it ever comes up.

>   Arrays (either stack-allocated or dynamically allocated with 'new') are,
> however, guaranteed to be constructed in the order of increasing indexes
> and destroyed in the reverse order.

Oh, so arrays do destruct. That's good to know. I guess that makes sense, 
tho, now that I think about it, since the values get constructed if they 
have constructors.

-- 
Darren New, San Diego CA, USA (PST)
    C# - a language whose greatest drawback
    is that its best implementation comes
    from a company that doesn't hate Microsoft.


Post a reply to this message

From: John VanSickle
Subject: Re: C++ destructor question
Date: 6 Aug 2010 07:44:38
Message: <4c5bf5a6$1@news.povray.org>
Darren New wrote:
> Speaking of RAII and all...
> 
> When an object is stuck into a standard collection in C++, and then the 
> collection goes out of scope, what order is defined on the destructors 
> of the things in the collection? I wouldn't imagine it's the order they 
> were inserted (at least for a random-access type collection).  Does a 
> stack destruct in a different order than a queue does, for example? Does 
> a vector always destruct on increasing indexes?

See if you can discern the order of construction.  The safest 
destruction reverses this order.

Regards,
John


Post a reply to this message

From: Warp
Subject: Re: C++ destructor question
Date: 6 Aug 2010 07:54:08
Message: <4c5bf7e0@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Oh, so arrays do destruct. That's good to know. I guess that makes sense, 
> tho, now that I think about it, since the values get constructed if they 
> have constructors.

  You would have a pretty bad danger of memory leak if arrays wouldn't
destroy their elements.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: C++ destructor question
Date: 6 Aug 2010 11:24:47
Message: <4c5c293f$1@news.povray.org>
Warp wrote:
>   You would have a pretty bad danger of memory leak if arrays wouldn't
> destroy their elements.

Yeah, I just wasn't thinking clearly, thinking "arrays aren't classes, so 
they don't have destructors."

-- 
Darren New, San Diego CA, USA (PST)
    C# - a language whose greatest drawback
    is that its best implementation comes
    from a company that doesn't hate Microsoft.


Post a reply to this message

From: Warp
Subject: Re: C++ destructor question
Date: 6 Aug 2010 13:42:12
Message: <4c5c4974@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> >   You would have a pretty bad danger of memory leak if arrays wouldn't
> > destroy their elements.

> Yeah, I just wasn't thinking clearly, thinking "arrays aren't classes, so 
> they don't have destructors."

  Curiously, *all* primitive types in C++ have default constructors. At
least in syntax. (This default constructor initializes the variable to zero.)
It can be called like any other constructor. So you could have something
like:

    void foo(int i = int()) { ... }

  In other words, the foo() function takes optionally an int as parameter,
and if none is specified, it takes a default-initialized int instead.

  Of course the above is *completely* equivalent to:

    void foo(int i = 0) { ... }

so what's the point? It becomes important with (yeah, you guessed it)
templates. You can write something like:

    template<typename T>
    class MyClass
    {
     public:
        void foo(T value = T());
    };

and it will work even if T is a primitive type (such as int).

  (The default function parameter value is just one example of many
situations where it becomes useful.)

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.