c++ or c#

Started by
39 comments, last by sebastiansylvan 17 years, 11 months ago
Quote:Original post by Surg
Sure you can make memmory leaks, if you don't know what you are doing, but a simple garbage collection system isn't hard to make, and it can be customized more.


I find this comment to be offensive on so many levels. First of all it's quite arrogant. You're basically saying "yeah it's easier to make misstakes in C++, but I'm smart enough to never make misstakes". Here's a hint for you: No you're not. Nobody's smart enough to never make misstakes. You may make fewer of them in a given time-frame, but that doesn't mean you wouldn't benefit just as much as a complete moron (relatively speaking) from features which help reduce errors.

Managed memory is The Right Thing for the future. Whether it's by garbage collection, region inference or any technique not invented yet. Manual memory managed is inefficient, unproductive and unsafe, it should be phased out as soon as possible. Period.

The second part of your comment is just inaccurate. No, you can not write a garbage collection system in C++. Consider the following code

 MyObj* pObj = new MyObj(); int p = reinterpret_cast<int>(pObj); // .. much later .. MyObj* newpObj = reinterpret_cast<MyObj*>(p);


What happens if a GC occurs in between theses two code snippets? The run-time system has no way of knowing that you are actually holding a "pointer" (by means of an int) to the MyObj object and may delete it, or it may simply relocate it, only updating the adress in pObj but not p (since p is an int, not a pointer).

You can implement crude and inefficient reference counting schemes (which can't handle cyclic structures) and non-relocatng mark-and-sweep garbage collection (which fragments the memory and is really slow), but you can not do anything which rivals the sophistication and speed of the garbage collection in .Net or Java.

Oh, and just to let you know, heap allocation/deallocation is usually about 20x faster in C#/Java compared to C++. Just in case you were thinking about bringing up a ficticious performance argument (you can easily create a quick-n-dirty benchmark to test this yourself). C++ is often faster, but mostly due to managed languages having more runtime checks (which can be turned off for select parts of the program in C#) and the fact that C++ programs typically allocate on the stack more often, but not because managed memory is somehow inherently inefficient (it isn't, it's actually faster in most cases).

[Edited by - sebastiansylvan on May 6, 2006 3:01:31 PM]

This topic is closed to new replies.

Advertisement