POV-Ray : Newsgroups : povray.off-topic : C++ questions Server Time
7 Sep 2024 09:20:37 EDT (-0400)
  C++ questions (Message 41 to 50 of 123)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Invisible
Subject: Re: C++ questions
Date: 26 Sep 2008 11:31:44
Message: <48dd0060@news.povray.org>
Warp wrote:

>   The class constructors, assignment and destructor help in automatizing
> the internal memory management of the vector.
> 
>   You can write your own dynamic data container as a class in the exact
> same way as std::vector and the other STL containers, which will be safe
> and will automatically manage its memory.

But presumable you'd need to know what you're doing first. ;-)

>   And not only memory. There are also other resources which can be
> automatically managed with classes.

Mmm, that's nice.

>   Unless I'm completely mistaken, this is not something you can achieve
> in Java. (In Java you have to close files explicitly and catch all
> possible exceptions which might otherwise bypass that closing.)

Actually, for reasons beyond my comprehension, if a Haskell program 
halts abnormally, all files are closed, but not necessarily *flushed*. 
Like, WTF? *Why?* Anyway, basically you have to write some code to catch 
the exception and close the file before rethrowing it. It's an extra 3 
lines of code, but you still have to remember to type them...

>> I've never actually seen a programming environment with manual memory 
>> management *and* dynamically resizable collections. The two are usually 
>> muturally exclusive.
> 
>   Well, it's not like *all* resources in C++ are managed manually.
> More precisely, stack-allocated objects are managed automatically. Which
> is exactly the reason why automatic management heap-allocated objects
> becomes possible through these stack-allocated objects.

Indeed. I've not seen this in any other OO language.

>> Then again, I'm not really sure what the story is if you try to put 
>> user-defined data types into a vector...
> 
>   If the user-defined type doesn't allocate memory in itself, or if it
> does but manages it properly, there shouldn't be any problems.

Sounds good to me! ;-)


Post a reply to this message

From: Darren New
Subject: Re: C++ questions
Date: 26 Sep 2008 12:31:06
Message: <48dd0e4a$1@news.povray.org>
Invisible wrote:
> Looks like it would only be useful for some pretty obscure situations...

Really? They're useful all the time in C#. Like, every time you have a 
callback function you register with the GUI, or every time in Java where 
you'd have to create an anonymous inner class or something. Pointers to 
members are the OO mechanism corresponding to closures.

You have a window representing an object. You want to be notified 
whenever the mouse moves over the window. How do you tell the window 
manager that?

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

From: Darren New
Subject: Re: C++ questions
Date: 26 Sep 2008 12:35:35
Message: <48dd0f57$1@news.povray.org>
Warp wrote:
>   Unless I'm completely mistaken, this is not something you can achieve
> in Java. (In Java you have to close files explicitly and catch all
> possible exceptions which might otherwise bypass that closing.)

The destructor will close the file when the GC collects the object, but 
that might be several seconds later. And it might not run if you exit 
the program before the GC runs, but that's true of C++ also. So it's not 
quite as nicely deterministic, but it still happens eventually.

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

From: Darren New
Subject: Re: C++ questions
Date: 26 Sep 2008 12:37:17
Message: <48dd0fbd$1@news.povray.org>
Invisible wrote:
> Actually, for reasons beyond my comprehension, if a Haskell program 
> halts abnormally, all files are closed, but not necessarily *flushed*. 

Because it's Haskell that flushes the buffers to the file, but it's the 
OS that closes the file. If you run it on (say) an Amiga, it won't close 
the files, either. Apparently Haskell isn't writing directly to the 
files, but to Haskell-managed buffers. Same thing happens in C with stdio.

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

From: Mike Raiford
Subject: Re: C++ questions
Date: 26 Sep 2008 12:42:16
Message: <48dd10e8@news.povray.org>
Darren New wrote:

> 
> The destructor will close the file when the GC collects the object, but 
> that might be several seconds later. And it might not run if you exit 
> the program before the GC runs, but that's true of C++ also. So it's not 
> quite as nicely deterministic, but it still happens eventually.
> 

C# has the same issue. You can sort of work around it with the using 
statement, which will call Dispose on an IDisposable object, which, in 
turn, closes the file.

-- 
~Mike


Post a reply to this message

From: Darren New
Subject: Re: C++ questions
Date: 26 Sep 2008 13:09:15
Message: <48dd173b$1@news.povray.org>
Mike Raiford wrote:
> Darren New wrote:
> 
>>
>> The destructor will close the file when the GC collects the object, 
>> but that might be several seconds later. And it might not run if you 
>> exit the program before the GC runs, but that's true of C++ also. So 
>> it's not quite as nicely deterministic, but it still happens eventually.
>>
> 
> C# has the same issue. You can sort of work around it with the using 
> statement, which will call Dispose on an IDisposable object, which, in 
> turn, closes the file.

Yep. Of course, it's really not a problem in a system where the kernel 
participates in the GC, like Singularity does.

And, really, memory is a huge percentage of the resources you manage, so 
the small bit of "real" resources you have to handle manually is usually 
not much of a problem. Indeed, only once did I have code sufficiently 
convoluted and error-prone that I had a resource leak of this type in 
Tcl. And even then, it's because I wrote the code poorly and instead of 
(say) wrapping the allocation and use in a procedure call, I tried to 
handle it all inline.

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

From: Orchid XP v8
Subject: Re: C++ questions
Date: 26 Sep 2008 13:31:59
Message: <48dd1c8f$1@news.povray.org>
Darren New wrote:
> Invisible wrote:
>> Actually, for reasons beyond my comprehension, if a Haskell program 
>> halts abnormally, all files are closed, but not necessarily *flushed*. 
> 
> Because it's Haskell that flushes the buffers to the file, but it's the 
> OS that closes the file. If you run it on (say) an Amiga, it won't close 
> the files, either. Apparently Haskell isn't writing directly to the 
> files, but to Haskell-managed buffers. Same thing happens in C with stdio.

Yeah, but if the Haskell runtime has code to handle printing out a 
human-readable error message on an unhandled exception, why can't it 
also run some finalisers? This is pretty basic stuff!

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Fredrik Eriksson
Subject: Re: C++ questions
Date: 26 Sep 2008 13:32:49
Message: <op.uh3m0zuk7bxctx@e6600>
On Fri, 26 Sep 2008 16:30:35 +0200, Invisible <voi### [at] devnull> wrote:
>>    I think the only operator you can't overload is the dot operator.
>
> And .* (apparently).

:: and ?: cannot be overloaded either.


-- 
FE


Post a reply to this message

From: Warp
Subject: Re: C++ questions
Date: 26 Sep 2008 13:38:42
Message: <48dd1e22@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Invisible wrote:
> > Looks like it would only be useful for some pretty obscure situations...

> Really? They're useful all the time in C#. Like, every time you have a 
> callback function you register with the GUI, or every time in Java where 
> you'd have to create an anonymous inner class or something. Pointers to 
> members are the OO mechanism corresponding to closures.

> You have a window representing an object. You want to be notified 
> whenever the mouse moves over the window. How do you tell the window 
> manager that?

  Isn't this usually handled with callback interfaces (in OO languages)
rather than function pointers?

-- 
                                                          - Warp


Post a reply to this message

From: Orchid XP v8
Subject: Re: C++ questions
Date: 26 Sep 2008 13:44:50
Message: <48dd1f92$1@news.povray.org>
Fredrik Eriksson wrote:
> On Fri, 26 Sep 2008 16:30:35 +0200, Invisible <voi### [at] devnull> wrote:
>>>    I think the only operator you can't overload is the dot operator.
>>
>> And .* (apparently).
> 
> :: and ?: cannot be overloaded either.

That seems logical...

I bet you can't overload ";", can you? In Haskell, you can. >:-D

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

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