Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Julio Di Egidio Newsgroups: comp.lang.c Subject: Re: So You Think You Can Const? Date: Wed, 8 Jan 2025 17:24:05 +0100 Organization: A noiseless patient Spider Lines: 63 Message-ID: References: <20250107130809.661@kylheku.com> <87a5c15ob0.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 08 Jan 2025 17:24:06 +0100 (CET) Injection-Info: dont-email.me; posting-host="2a46dd651168680e2ad36d621d0ac75a"; logging-data="2544429"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+6W3AsNNpp3jiQj3TjmUN5odwNH/SWblY=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:C5gAkpsbP2bHDAI4mfd0vjSLT1Y= Content-Language: en-GB In-Reply-To: Bytes: 2922 On 08/01/2025 17:05, Julio Di Egidio wrote: > On 08/01/2025 16:16, Ben Bacarisse wrote: >> More relevant to a discussion of const is to ask what you plan to do >> with pT since you can't (without a cast) assign any useful value to the >> allocated object. > > Say my program unit implements AVL trees, with (conceptually speaking) > constructors/destructors, navigation and retrieval, and of course > manipulation (inserting, deleting, etc.). > > My idea (but I would think this is pretty "canonical" and, if it isn't, > I am missing the mark) is: my public functions take/give "sealed" > instances (with const members to const data), as the user is not > supposed to directly manipulate/edit the data, OTOH of course my > implementation is all about in-place editing... P.S. To be clear, as I am still being a bit quick: I do not also mean "public destructors" should take a const pointer in input, i.e. apply as appropriate... And here is what my construction/destruction code is looking like at the moment, which should also make clear what I meant by "a private method implementing a public interface" and why: ```c static AvlTree_t const *AvlTree_node( void const *pk, AvlTree_t const *pL, AvlTree_t const *pR ) { AvlTree_t *pT; pT = malloc(sizeof(AvlTree_t)); if (!pT) { return NULL; } pT->pk = pk; pT->pL = pL; pT->pR = pR; return pT; } static int AvlTree_free_(AvlTree_t const *pT) { assert(pT); free((AvlTree_t *)pT); return 0; } AvlTree_t const *AvlTree_create(void const *pk) { return AvlTree_node(pk, NULL, NULL); } void AvlTree_destroy(AvlTree_t *pT) { AvlTree_visitPost(AvlTree_free_, pT); } ``` -Julio