Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Latest Newsgroups: comp.lang.c++,comp.lang.c Subject: Re: No, C is not a simple language Followup-To: comp.lang.c Date: 26 Apr 2021 09:02:04 GMT Lines: 57 Message-ID: References: X-Trace: individual.net eVLgQz4k2mJ65bwZ65yLCghJwEy89RsF2IDsGnaLnX76fbfOaV Cancel-Lock: sha1:VHv7DCTvEe9UH5sFGnd3HVQ20AU= User-Agent: slrn/1.0.3 (Linux) Xref: csiph.com comp.lang.c++:79274 comp.lang.c:160345 ["Followup-To:" header set to comp.lang.c.] Juha Nieminen wrote: > The problem with C is that anything that requires manual construction and > destruction will "contaminate" (can't think of a better word) anything that > uses that first thing, with the same requirements. All the way down. Of course it does. If you insist on doing it in C, it's best to define functions new_xxx and free_xxx for each type xxx, and make sure that free_yyy is called from free_xxx if the xxx type contains dynamically allocated objects of type yyy. It's really not hard, just requires a bit of typing, but it's a tremendously helpful exercise especially for a beginner. BTW if this concept is new or "The problem" for you, I don't believe you have much experience with C. > If you have this kind of "string" object that requires manual construction > and destruction, and you put it as a member of a struct, suddenly that > struct "inherits" the same requirement. Use that struct in yet another > struct and that, too, will "inherit" the same requirement. Trivially dealt with, see above. > Moreover, suppose you have a complex hierarchy of structs as members of > other structs, no dynamic memory allocation in sight so far. Then you need > to add dynamic memory allocation to one of the structs (that's being used > as a member of other structs etc) and suddenly all that code breaks. ...as it would in any language if you change the architecture of your design without making the appropriate changes where needed. > If > there are millions of lines of code using any of those structs that have > now "inherited" the need for manual construction and destruction (even though > they were never designed for that initially), you'll have to go and modify > those millions of lines to follow suit. If that's a problem for you, just don't use a strongly typed language. > Not so in C++, if you design your structs/classes correctly. Your argument above is based on NOT designing your data structures correctly in C, and now you say C++ handles better but only IF you do it correctly? Doesn't make sense. Even when trying to do everything explicitly right in C you will make mistakes. Or I will, anyway. I've found valgrind to be an invaluable tool when debugging C code. I'm very happy with teaming up C and Python. Low level ultrafast stuff in C extension, high level OO garbage-collected stuff in Python. In the 90's I did a largish project in C++ and never grew to like it much. -- robert