Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++,comp.lang.c Subject: Re: No, C is not a simple language Date: Sun, 18 Apr 2021 12:46:06 -0700 Organization: None to speak of Lines: 45 Message-ID: <878s5f1im9.fsf@nosuchdomain.example.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="d89a283199d04e2707da948b66e2b255"; logging-data="7333"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184ueHhiwwYmJtX4GYe8yIf" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cancel-Lock: sha1:b3l3W/Uj8AIJaa3WJkrj/rLygnA= sha1:41oNEIlExvMuPp6hBFAZQrKKlr0= Xref: csiph.com comp.lang.c++:79052 comp.lang.c:160200 Manfred writes: > On 4/18/2021 6:46 PM, Juha Nieminen wrote: >> In comp.lang.c++ Manfred wrote: >>> For example, at a first glance the design of string object is not that >>> great for C. >>> A common pattern that pops to the mind is to use opaque structs (see >>> e.g. FILE*), and have something like: >>> >>> typedef struct string_t STRING; >>> >>> STRING* alloc_string(); // creates the empty string >>> STRING* set_string(STRING* s, const char* value); >>> void free_string(STRING* s); >>> STRING* concat_string(STRING* s1, STRING* s2); >>> ... >>> >>> This way there would be no ambiguity about how the STRING object has to >>> be used. >> >> I see no practical difference between that and a String struct. The usage is >> pretty much the same, and all the same problems appear (such as assigning >> one string object/pointer to another requiring extra care to avoid destroying >> the string twice or accessing a destroyed string). >> > > If the struct is opaque, the compiler won't allow you to assign a > STRING object to another, as a start. In fact it won't allow to > perform /any/ operation on the object, except through the API that you > define. > > You can still mess up with pointers, but if you want to use C, then > you have to know better. > > This is nothing new, as I wrote the entire FILE* infrastructure works > this way. Except that you *can* assign objects of type FILE (if you don't mind undefined behavior). The FILE type is opaque in the sense that the standard doesn't say what it looks like, but it has to be an object type. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */