since i am not very comfortable with c++ way of manually managing the dynamic memory. i was
thinking if there is a c++ compiler or something like that that would provide me garbage collection
in c++ without any extra efforts in coding on my part.
since i am not very comfortable with c++ way of manually managing the dynamic memory. i was
thinking if there is a c++ compiler or something like that that would provide me garbage collection
in c++ without any extra efforts in coding on my part.
I suggest that you read Stroustrup's answer to the FAQ: How do I deal with memory leaks?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
There are two things to consider here:
1. C++ was originally created by Bjarne Stroustrup because the garbage collection in Simula-67 that he originally wrote the code in was taking some 80% of the CPU time when running a simulation of some sort. Hence he rewrote the Simula code using a special parser that created standard C code - this later became "Cfront", which was the first C++ "compiler".
2. There are ways to avoid quite a bit of manual freeing of data, for example using Smart Pointer classes.
Or you could use Java or C# - both of these languages have gc as part of the language.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
thanks laserlight and mats!
i had almost made up my mind to switch to java. i downloaded the jdk6. ran the programs. and boy!
they were so slow i could take a nap in between (ok, that's too much, sorry!).
since all of java is byte code. but all of c++ is native code. so a gc will not affect much in c++ case.
i have read that article.
in my project there are 1147 new and 447 delete. >_<
and they can't be avoided (not easy, nope! *_*).
may be there is some compiler support for gc. does g++ do it?
Well, that you have less delete than new means that somewhere, the use of delete is being hidden (assuming that you are not simply failing to match new with delete).in my project there are 1147 new and 447 delete. >_<
and they can't be avoided (not easy, nope! *_*).
So yes, they can be avoided (the deletes, at least), though if you did not write the code with RAII in mind, it may not be trivial to convert.
Stroustrup himself points out that garbage collection libraries are available for C++.Use smart pointers. There is no garbage collection for C++.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I'm not aware of any gc capable C++ compatible compiler.
Having an unequal number of new and delete is, in itself, not a problem, consider this:
But you should of course delete every object you create, and in large projects, this CAN be difficult - but encapsulating the new/delete in classes, that is a way to handle it.Code:if (a) b = new base; else b = new derived; ... delete b
Another way is to have your own new/delete functions [for debug purposes], and write some extra code in places to check if you have more new than delete when you expect the code to be "neutral", so for example call the function that builds a binary tree from an input file, then call the teardown of the same function and check that you have the same number of allocated objects.
Yes, it's extra work, but it's not an insurmountable task to achieve this.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
thanks elysia!
i found this example on boost. can i do here this instead?Code:truct Shoe { ~Shoe() { std::cout << "Buckle my shoe\n"; } }; int main() { boost::scoped_ptr<Shoe> x(new Shoe); return 0; }
boost::scoped_prt<Shoe> x = new Shoe;
this is more clean syntax imo. is there an error using this syntax?
thanks pheres! i think this is what i wanted! they saay it's a bit harder to use it for c++. but i hope it's not too
much work. otherwise i have no problem discarding the use of free/malloc and use GC_MALLOC/GC_FREE instead!
I do think you should give boost.smart_ptr library a try. It solves most (not all) of the problems you may encounter with memory allocation and, it seems to me, more easy to use than the also famous hans bohem garbage collector (libgc). Part of this boost library was also added to the TR1, which may mean an inclusion on the upcoming standard library. This will allow you to use a library that shares a commnon semantic background with the standard library.
I think it would be good for you to also implement your own smart pointers to better understand some of the issues. Reference counting in a particular is a powerful technique - and quite an interesting one too - since it will give you ideas for many other things not smart-pointers related. I know it has to me.
The best description of reference counting I've seen so far was in the book C++ Primer, 4th Edition from Addison Wesley. It explain the technique in an easy to use language and in good detail. If you can't afford to get a new book right now... search google for "C++ RAII" and "C++ reference count". There is bound to exist a good tutorial on the technique.
Posting here as you try to implement your own reference count class will earn you extra miles in the knowledge you will acquire.
Really, I do think RAII is one of the most important things one should learn about when programming in C++. It's da Design Pattern one should learn before learning any other. Many book authors ignore the subject almost entirely, and yet this is one of the fundamental concepts of the language. In won't be enough to just use someone else's garbage collector (be it boost or libgc).
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
The main problem with GC is that you lose deterministic destruction. In C++, many classes rely on deterministic destruction for timely cleanup of resources.
Boehm's GC library doesn't call destructors at all, I think. Read this:
http://www.hpl.hp.com/personal/Hans_...alization.html
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
That reminds me reading a short time ago about that being a common problem of GC libraries. An article by Herb Sutter, if memory serves me right.
Couldn't follow the article in its entirety (I lack the background knowledge for that), but got the impression this happens because of how finalizers are used to bind destructors to GC, and he advertises this as wrong since, particularly to local objects if they lose scope, we do want them to be destroyed that instant.
Meanwhile any performance considerations - which seems the real reason behind non-deterministic destruction proponents - holds little value because that performance degradation that was avoided will resurface somewhere else... and it allows us to write code that is conceptually wrong.
All in all I got the impression that he was saying GC is really not for C++. We should stick to deterministic destruction.
EDIT: If possible, I would like to know a little more about this
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.