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, 15 Jan 2025 14:15:53 +0100 Organization: A noiseless patient Spider Lines: 76 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, 15 Jan 2025 14:15:55 +0100 (CET) Injection-Info: dont-email.me; posting-host="2563c1350cfe2ddd1cd2f0f3cf044725"; logging-data="3106880"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18gy0BXGPMwGgUoKh6buZ78Zu9vT04pwAo=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:Ev0cFgsU/nZ4Se2BjS2UP6Mgmcw= Content-Language: en-GB In-Reply-To: <87a5c15ob0.fsf@bsb.me.uk> Bytes: 3659 On 08/01/2025 16:16, Ben Bacarisse wrote: > Julio Di Egidio writes: > >> Overall, I am surmising this and only this might go write-protected: >> >> MyStruct_t const T = {...}; > > Yes, though you should extend your concern beyond what might be You piece of fraudulent, incompetent, then always nazi-retarded shit, now you get it? No, since you incompetent piece of shit indeed don't even know half of it. I just hope you aren't teaching, you piece of ungodly shit. Ah, and I was just quoting from an answer I had got, you piece of nazi-retarded ungodly fraudulent nazi-retarded shit... -Julio > write-protected. Modifying an object whose type is const qualified is > undefined, even if the object is in writable storage. A compiler may > assume that such an object has not changed because in a program that has > undefined behaviour, all bets are off. For example, under gcc with > almost any optimisation this program prints 42: > > #include > > void f(const int *ip) > { > *(int *)ip = 0; > } > > int main(void) > { > const int a = 42; > f(&a); > printf("%d\n", a); > } > >> While this one allocates a "byte-array", i.e. irrespective of how the >> pointer we are assigning it is declared: >> >> MyStruct_t const *pT = malloc(...); >> >> Is my understanding (to that point) correct? > > Technically you get an object with no effective type. David's reply > included some references to find out more about the effective type of an > object, but it is safe to say that these only come into play if you are > messing about with the way you access the allocated storage (for example > accessing it as a MyStruct but then later as a floating point object). > > 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. > > It is generally better to use a non const-qualified pointer for the > allocation but, when using the pointer, to pass it to functions that use > the right type depending on whether they modify the pointed-to object or > not. For example: > > MyStack *sp = malloc(*sp); > ... > stack_push(sp, 99); > ... > if (stack_empty(sp)) ... > ... > stack_free(sp); > > we would have > > void stack_push(MyStack *sp, int v) { ... } > bool stack_empty(MyStack const *sp) { ... } > void stack_free(MyStack *sp) { ... } >