Home » Community » Coffee corner » Basic questions about u++
Re: Basic questions about u++ [message #23302 is a reply to message #23265] |
Wed, 07 October 2009 21:28 |
mr_ped
Messages: 825 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
The C++ compiler does call destructors when the object goes out of scope. The resources used by that object are not freed magically, you have to write the destructor code. Then it is "magically" called whenever that object goes out of scope.
Probably everything you will use from U++ does already have proper destructor, so you can write it as Mirek posted, you just create new object on heap, and when you go out of scope, you know it was destroyed properly.
In case you use "new" in code (and really can't avoid it, which is usually quite easy with U++ way of doing things), you have either to detect it in destructor and call appropriate "delete" (a good idea which keeps your custom classes to behave same as U++ things) or you have to watch out during that scope and call "delete" by hand whenever it is needed (going out of scope) (quite error prone).
C++ of course calls ctors/dtors automagically (inserting those calls at proper places during compilation) just like you would expect it (well, almost) (see C++ standard), so if you manage your resources this way, you can avoid many common bugs which usually arise from new/delete in function code.
But in the end it's yours (and U++) code responsibility to free everything correctly. If you do it in U++ way, you will very rarely have to think about it, it will work almost "alone".
But still you should understand how it works and why, so you don't allocate for example 3 big memory blocks at the same time (same scope), when you need just 1 at a time and then you can release it and allocate another one (but this is also problem for GC languages, if you are way too much careless, you will degrade performance of your code tenfold, and GC will not save you).
|
|
|
|
|
Basic questions about u++
By: irtech on Tue, 06 October 2009 14:14
|
|
|
Re: Basic questions about u++
By: mr_ped on Tue, 06 October 2009 14:41
|
|
|
Re: Basic questions about u++
By: mr_ped on Tue, 06 October 2009 15:03
|
|
|
Re: Basic questions about u++
By: irtech on Tue, 06 October 2009 15:30
|
|
|
Re: Basic questions about u++
By: zsolt on Tue, 06 October 2009 15:51
|
|
|
Re: Basic questions about u++
By: irtech on Wed, 07 October 2009 06:19
|
|
|
Re: Basic questions about u++
|
|
|
Re: Basic questions about u++
By: mirek on Wed, 07 October 2009 08:29
|
|
|
Re: Basic questions about u++
By: mirek on Wed, 07 October 2009 08:46
|
|
|
Re: Basic questions about u++
By: mr_ped on Wed, 07 October 2009 09:04
|
|
|
Re: Basic questions about u++
By: mirek on Wed, 07 October 2009 09:46
|
|
|
Re: Basic questions about u++
By: irtech on Wed, 07 October 2009 10:04
|
|
|
Re: Basic questions about u++
|
|
|
Re: Basic questions about u++
By: mr_ped on Wed, 07 October 2009 10:45
|
|
|
Re: Basic questions about u++
By: unodgs on Wed, 07 October 2009 11:54
|
|
|
Re: Basic questions about u++
By: irtech on Wed, 07 October 2009 12:11
|
|
|
Re: Basic questions about u++
By: mirek on Wed, 07 October 2009 12:23
|
|
|
Re: Basic questions about u++
By: mr_ped on Wed, 07 October 2009 13:33
|
|
|
Re: Basic questions about u++
By: irtech on Wed, 07 October 2009 16:18
|
|
|
Re: Basic questions about u++
By: chickenk on Wed, 07 October 2009 17:46
|
|
|
Re: Basic questions about u++
By: mrjt on Wed, 07 October 2009 18:14
|
|
|
Re: Basic questions about u++
By: irtech on Wed, 07 October 2009 18:47
|
|
|
Re: Basic questions about u++
By: mrjt on Wed, 07 October 2009 19:07
|
|
|
Re: Basic questions about u++
By: mirek on Wed, 07 October 2009 19:14
|
|
|
Re: Basic questions about u++
By: mr_ped on Wed, 07 October 2009 21:28
|
Goto Forum:
Current Time: Mon May 13 06:25:04 CEST 2024
Total time taken to generate the page: 0.02997 seconds
|