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: So You Think You Can Const? Date: Tue, 7 Jan 2025 20:32:50 +0100 Organization: A noiseless patient Spider Lines: 48 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 07 Jan 2025 20:32:51 +0100 (CET) Injection-Info: dont-email.me; posting-host="6aa9c0e90d2516bf9182ef85056c691c"; logging-data="2349973"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18mOBx/7yuwaMcU14pwqXcCv1HeCaHMeTE=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:4qstCk47V5+Mf7a8b+n3H7X9uSg= Content-Language: en-GB Bytes: 2370 Hi everybody, I am back to programming in C after many years: indeed I have forgotten so many things, including how much I love this language. :) In particular, I am using C90, and compiling with `gcc ... -ansi -pedantic -Wall -Wextra` (as I have the requirement to ideally support any device). To the question, I was reading this, but I am not sure what the quoted passage means: Matt Stancliff, "So You Think You Can Const?", << Your compiler, at its discretion, may also choose to place any const declarations in read-only storage, so if you attempt to hack around the const blocks, you could get undefined behavior. >> I do not understand if just declaring that a pointer is to constant data may incur in that problem even if the pointed data was in fact allocated with malloc. I would say of course not, but I am not sure. E.g. consider this little internal helper of mine (which implements an interface that is public to do an internal thing...), where I am casting to pointer to non-constant data in order to free the pointed data (i.e. without warning): ```c static int MyStruct_free_(MyStruct_t const *pT) { assert(pT); free((MyStruct_t *)pT); return 0; } ``` Assuming, as said, that the data was originally allocated with malloc, is that code safe or something can go wrong even in that case? Thank in advance for any help/insight, Julio