why is c# better for game development than c++(you can be vague)

Started by
298 comments, last by normalme 19 years, 10 months ago
quote:Original post by nyugfu
>> Lack of garbage collection which makes certain idioms problematic, and causes developers to have to waste time dealing with memory allocation and ownership issues, rather than focusing on the actual problem they are trying to solve.

This is true, but I would argue that C# can still require you to focus on a whole new set of issues as well, as davepermen pointed out. The inability to explicitly control the allocation and deallocation of an object requires you to track down every single reference if you want to avoid internal memory leaks, which I imagine can be quite challenging in a large scale system.

However, this problem isn't new , it's the same problem you have in C++. In C++, you still need to track every single reference if you are to ensure that you don't forget to deallocate something that is later not referenced.

Also, in C# it is possible in principle to profile the program to detect references that aren't being used. The profiler could then warn you that a particular reference may be 'leaky'.
quote:
I don't think this creates any more confusion than in C#. The behavior is simply reversed. C#, by default, passes things by reference while C++ passes objects by value.

Well, that's not true. C# classes are references, parameters are actually passed by value, just as in C++.
quote:
A programmer should still think about whether he/she wishes to deep copy or shallow copy an object. In fact, if he/she doesn't know well enough to decide, I would think that it would be less confusing and potentially less problematic to pass and return copies around.

Which is a little easier in C# than in C++.
quote:
It's worth mentioning that a problem with many C++ programs is that many programmers do a lot of unnecessary copying.

In my experience, a primary reason for this is that it's a lot easier to copy data than to deal with the memory management issues of references into heap allocated objects.
quote:
However, I would still bet that many of them outperform similar C# programs. It's when a programmer shallow copies an object and hopes to be working with a copy when the bigger, more difficult problems arise.

If the programmer "hopes to be working with a copy", then he should know that he needs to deep copy the object when making a copy. Programmer error, not language error.
quote:
Additionally, I think some of your points are really IDE-specific (though you're probably perfectly aware of it). While C++ IDEs at the present may have synchronization issues between headers and implementations (I avoid most fancy IDEs in C++ so I wouldn't know), that's still a feature of the IDEs and not C++. It doesn't mean it would be impossible for an IDE to be created that would keep the things in sync.

Indeed. Synchronisation between headers and implementations isn't a feature of C++ at all. That is a Bad Thing. A language which automatically generated headers from its implementation would be a better idea.
quote:
The form designer is not part of the C# language.

Hmm. So flaws in C# are not solved by an IDE, whilst flaws in C++ are solved by an IDE?
quote:
Also, you mentioned C++'s longer build times. This is certainly a disadvantage, but it's due to the fact that C++'s power comes mainly from its static, compile-time capabilities. This is why applications using templates as a means of generic design can greatly outperform C# applications which use object boxing. Templates slow down compile time but have no effect on the application's performance at runtime.

But the fact of C++'s build times remains. And of course, C# 2.0 supports generics and still manages to compile quickly.
quote:
On a similar note, most of the commonly used C++ libraries are statically compiled and linked, including the STL (and for good reason). This contributes to a great portion of the build time. If you wanted, you could certainly take the functionality from these libraries and compile your own dynamic linked libraries and avoid the lengthy build times that way, as C# does. However, you would have to sacrifice the use of templates and use other techniques for generic programming which would inevitably lead to a reduction in efficiency. If C# had to statically compile and link the full .NET framework, the build times would be horrific and the executables would be huge.

Yes. It would also be stupid. Handily, the CIL itself is highly generic.

For example, the function T add (T a, T b) { a + b} can be compiled to precisely two functions -- one that adds primitive values of any type, and one that invokes operator+ on Object values. When the CIL is recompiled into native code at load (or preload) time, the generic function can be compiled into whatever distinct functions are needed.

Edit: The reason I mention this is that I figure this makes precompiled generics libraries more useful in C# than in C++. Although that does mean people who would have distributed a C++ template library in source form might instead distribute a C# generics library in binary form. I don't like that.

In principle, it could be compiled to only one function, but the standard doesn't allow that.
quote:
From my standpoint, I can always make a C++ program compile faster at the cost of runtime performance or by writing a bunch of DLLs. There are also a lot of techniques such as pimpls to reduce compile-time dependencies. However, in C#, I have no choice but to do a lot of things dynamically which I could have done statically to get a more efficient program.

In terms of importance, however, I usually find that greater efficiency at runtime generally outweights the speed at which a program can be compiled and linked. A lot of people say that computers these days have so much memory and so much speed that programmers need not worry about such things, but there are still a number of applications where performance is crucial. Server-side applications, for instance, often need optimal performance to serve as many people as possible with a given set of hardware.

However, because C# is, in effect, compiled at load-time, it can be statically linked right then. The loader can also perform full-program optimisation, which is usually better than the single-module optimisation performed by traditional compilers.

If you don't like all this load-time delay (I know I wouldn't), you can prerecompile your program. Also, I assume that, eventually, the system will cache the recompiled programs so they don't have to be recompiled every time you run them.
quote:
However, just taking out features from a language because a lot of people misuse them is not always the best solution, in my opinion. This is the big problem I see in C# and Java's approach to 'better design'. It might make the general population design better, but it restricts the more advanced people from doing a lot of good stuff.

I agree absolutely. The removal of some cool-but-misused features, together with the fact that it is controlled by an organisation with an ulterior motive, are what I have against C#.

[edited by - Mayrel on February 19, 2004 9:27:06 PM]
CoV
Advertisement
Hey Mayrel, that's good that we agree on the final point, which is the most important one in my opinion.

quote:Original post by Mayrel Well, that's not true. C# classes are references, parameters are actually passed by value, just as in C++.


That's a sort of high-level perspective though, isn't it? A reference parameter in C++ is passed by value if you think of it as just a memory address being copied. At an even lower level, some systems just make the distinction between passing something by reference and by value where by value just has values copied back to the original. A smart pointer is, by default, passed by value and invokes the copy ctor of the smart pointer class, but it's still doing a shallow copy.

I guess what I meant to say is that C++ deep copies by default, while C# shallow copies.

quote:Original post by Mayrel Which is a little easier in C# than in C++.


I dunno about that. You have to explicitly invoke a Clone method, and implementing it for a class takes just as much thinking if not more than writing a copy ctor in C++.

quote:Original post by Mayrel In my experience, a primary reason for this is that it's a lot easier to copy data than to deal with the memory management issues of references into heap allocated objects.


I agree.

quote:Original post by Mayrel If the programmer "hopes to be working with a copy", then he should know that he needs to deep copy the object when making a copy. Programmer error, not language error.


Yeah, I got carried away there. Plus he/she would have to know how to write a copy ctor to do it properly any classes that use pointers.

I was thinking from a total newbie standpoint where a person that doesn't even know how to write classes might get more confused (I tutor people like this so I have a bad tendency to think too much from their perspective and I'm also taking a course that compares computer languages where the book mainly points out tiny things like this that would only confuse novices). I was really pushing it a bit there. Now that I think about it though, there are things like stream objects which tend to give unwanted behavior when deep copied (that's often a common source of confusion for novices to C++), so toss that point aside.

quote:Original post by Mayrel Indeed. Synchronisation between headers and implementations isn't a feature of C++ at all. That is a Bad Thing. A language which automatically generated headers from its implementation would be a better idea.


Yes, this would be nice. It's possible though, isn't it? Maybe it'll come around in time. I've also heard proposals about things related to this for the next standard revision, meaning it might even become a language feature that does not require an IDE.

quote:Original post by Mayrel Hmm. So flaws in C# are not solved by an IDE, whilst flaws in C++ are solved by an IDE?


All I meant to say there was that it could also be a pain to write C# programs (at least other than simple console applications) when you have to work without the visual studio IDE. There are faults like synchronization issues between declaring event callbacks and the event implementations in forms and controls if you make updates to one and not the other. I think IDE features should be omitted from this discussion, but I guess I did a poor job of trying to stress this point by getting into C#'s IDE problems in order to try to balance things out. I just meant that IDEs aside, both languages have some things that require you to write redundant code that shouldn't be necessary. Sorry about making that unclear.

quote:Original post by Mayrel But the fact of C++'s build times remains. And of course, C# 2.0 supports generics and still manages to compile quickly.


C# 2.0 might well be the answer to most of my gripes. I am looking forward to it very much, but my complaints so far have been simply over the versions of C# out so far.

quote:Original post by Mayrel Yes. It would also be stupid. Handily, the CIL itself is highly generic.

For example, the function T add (T a, T b) { a + b} can be compiled to precisely two functions -- one that adds primitive values of any type, and one that invokes operator+ on Object values. When the CIL is recompiled into native code at load (or preload) time, the generic function can be compiled into whatever distinct functions are needed.


I'm presuming you are talking about C# 2.0, which sounds like it will be great.

That example of using the STL functionality through DLLs, like my example of using a smart pointer for everything in C++, was just an attempt to just point out that it can be done, though most people purposely choose not to do it (for good reasons).

AFAIK, however, you can also get generic behavior out of COM DLLs in C++ by using polymorphism with a shared base (like object) as a means of implementing generics. It will not have the static power of templates, but if there are a lot of programs that use a very limited portion of the STL, it might not be that stupid to export some wrapper functions for it. You could always recompile the DLL if there are updates to the STL. I'd probably never do it myself though.

- David Ikeda

[edited by - nyugfu on February 19, 2004 11:04:52 PM]
quote:Original post by nyugfu
Hey Dave, have you ever run into a situation though where you needed complex interdependencies between smart pointers to a point where you were making circular references without being aware of it?


yes. happily my destructors by default all print out into the console so i realised quickly i missed something.

this was while writing a complex library, similar to the structure of dx. (refcounting is the way COM works, too.. at least).

at this point, i had to start working much with weak_ptr''s and all.. because some components could "bind" others, and all.. and they should all care about eachother to not make it possible one references to an invalid one.. and they should know wich "owns" it, too, so it stays alive as long as needed.

believe me, if you write some complex library where the user shouldn''t have to bother at all (dll functional C interface returning, in c++, only com_ptr style smart pointers, so that the user never has to bother at all), then memory management can get hell.

the small example i gave happens just much too often. libraries don''t have a final owner. they only own references to other components. it can happen very quickly one component circular references back at some point.

i like c++. i dislike some language issues. and i would love to be able to use gc in it by choise. the proposted managed c++ 2.0 with the Type^ pointers, wich are managed, and the Type* pointers, wich are not, sounds like interesting..

it''s a boring topic, memory management. and in c++, you have to do it a bit TOO manually, without choise to let it just happen. this would be nice.



If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

quote:Original post by nyugfu
1) A means of statically generating similar code with only small vartiations(i.e, templates)
2) The ability to mix procedural programming with object-oriented programming. Some may view this as a bad thing, but I think the C++ STL demonstrates how the two can work in harmony.

There are also a number of smaller things like multiple inheritance.

Like you said earlier, I also like to manage stuff myself sometimes where I know the lifetime and ownership. In fact, the majority of the time I want to be able to do this. I think the ideal solution would be to have the option of using GC without being required to. C# could just provide it as another type of smart pointer that has built-in language support for its garbage collection methods or work like managed C++ and let you use keywords to specify whether a certain class has automated garbage collection or not.


i guess you know D?



If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

quote:Original post by davepermen
i like c++. i dislike some language issues. and i would love to be able to use gc in it by choise. the proposted managed c++ 2.0 with the Type^ pointers, wich are managed, and the Type* pointers, wich are not, sounds like interesting..

Sounds a bit silly.

class Foo {  Foo () { barptr = new Bar; }  Bar *barptr;};class Bar {  Bar () { intptr = new int; }  int ^intptr;};void wibble () {  Foo *f = new Foo;} 


Here, the apparently managed intptr object will never get collected if barptr really isn''t managed. More sensible, IMO, would be to define * to be managed and have ^ (or an unsafe attribute) for unmanaged pointers. That way, you don''t need to go through your old C++ program replacing * with ^ everywhere.
CoV
i just stated one base thing of it. how it exactly is done, you can read up by yourself on the microsoft research pages. it''s en contraire rather well done, and not at all silly. they aren''t dump. and .NET exists now for a long while, gave them enough time to manage it.. hehe.

it''s quite an interesting concept actually. they try to manage mixing of managed and unmanaged code directly in one language.. looks like it works out very well, en contraire to your "silly".



If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Haven''t tried D yet. I''ve looked over the specs before but took a closer look today and it sounds really nice (the syntactical improvements really caught my attention). I''ll have to try it out some time.

quote:Original post by davepermen
quote:Original post by nyugfu
Hey Dave, have you ever run into a situation though where you needed complex interdependencies between smart pointers to a point where you were making circular references without being aware of it?


yes. happily my destructors by default all print out into the console so i realised quickly i missed something.

this was while writing a complex library, similar to the structure of dx. (refcounting is the way COM works, too.. at least).

at this point, i had to start working much with weak_ptr''s and all.. because some components could "bind" others, and all.. and they should all care about eachother to not make it possible one references to an invalid one.. and they should know wich "owns" it, too, so it stays alive as long as needed.

believe me, if you write some complex library where the user shouldn''t have to bother at all (dll functional C interface returning, in c++, only com_ptr style smart pointers, so that the user never has to bother at all), then memory management can get hell.


Ah, that sounds like a perfectly valid reason to expose the client to the smart pointers. The pain of writing dynamic link libraries has always been one of my biggest gripes with C/C++, especially in comparison to Java''s ability to load classes dynamically and .NET''s ability to so easily import and export class libraries.

I want something of a hybrid between the two. Maybe the next C# will closer along those lines.

It''s a shame how Java implemented generics recently. As far as I understand, it has just become a shorthand for object boxing and doesn''t really generate any type-specific code. I just hope C# doesn''t take the same route, but from what you guys have descibed of it, it sounds like it will be great.

- David Ikeda
Java is a web appplication platform newbie. The applications that do run on the PC on rough. Because of its structure it could not compare to any langauge devised for the PC platform itself.
quote:Original post by Anonymous Poster
Java is a web appplication platform newbie.

Umm.. so you''re stating it isn''t possible to write normal applications in Java?

quote:
The applications that do run on the PC on rough. Because of its structure it could not compare to any langauge devised for the PC platform itself.


???????

Necro - closed. Please only respond to relatively recent topics unless you have something of incredible importance to convey to the original poster.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

This topic is closed to new replies.

Advertisement