Re: So You Think You Can Const?

Liste des GroupesRevenir à cl c 
Sujet : Re: So You Think You Can Const?
De : ben (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.lang.c
Date : 08. Jan 2025, 16:16:03
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87a5c15ob0.fsf@bsb.me.uk>
References : 1 2 3
User-Agent : Gnus/5.13 (Gnus v5.13)
Julio Di Egidio <julio@diegidio.name> writes:

On 07/01/2025 23:11, Kaz Kylheku wrote:
On 2025-01-07, Julio Di Egidio <julio@diegidio.name> wrote:
<snipped>
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?",
<https://matt.sh/sytycc>
<< 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. >>
An object defined with a type that is const-qualified
could be put into write-protected storage.
>
What do you/we mean by "object" in this context?  (Sorry, I do have
forgotten, the glossary to begin with.)

An object (in C) is a contiguous region of storage, the contents of
which can represent values.

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
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 <stdio.h>
  
  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) { ... }

--
Ben.

Date Sujet#  Auteur
7 Jan 25 * So You Think You Can Const?174Julio Di Egidio
7 Jan 25 +* Re: So You Think You Can Const?80Kaz Kylheku
8 Jan 25 i`* Re: So You Think You Can Const?79Julio Di Egidio
8 Jan 25 i +- Re: So You Think You Can Const?1Julio Di Egidio
8 Jan 25 i `* Re: So You Think You Can Const?77Ben Bacarisse
8 Jan 25 i  +- Re: So You Think You Can Const?1David Brown
8 Jan 25 i  +* Re: So You Think You Can Const?72Julio Di Egidio
8 Jan 25 i  i+* Re: So You Think You Can Const?64Julio Di Egidio
8 Jan 25 i  ii+- Re: So You Think You Can Const?1Chris M. Thomasson
9 Jan 25 i  ii`* Re: So You Think You Can Const?62Ben Bacarisse
9 Jan 25 i  ii +* Re: So You Think You Can Const?4Kaz Kylheku
9 Jan 25 i  ii i`* Re: So You Think You Can Const?3David Brown
9 Jan 25 i  ii i +- Re: So You Think You Can Const?1Chris M. Thomasson
15 Jan 25 i  ii i `- Re: So You Think You Can Const?1Julio Di Egidio
9 Jan 25 i  ii +* Re: So You Think You Can Const?54Julio Di Egidio
10 Jan 25 i  ii i`* Re: So You Think You Can Const?53Ben Bacarisse
10 Jan 25 i  ii i +* Re: So You Think You Can Const?46Julio Di Egidio
10 Jan 25 i  ii i i+* Re: So You Think You Can Const?44Julio Di Egidio
10 Jan 25 i  ii i ii`* Re: So You Think You Can Const?43Tim Rentsch
10 Jan 25 i  ii i ii +* Re: So You Think You Can Const?38Julio Di Egidio
10 Jan 25 i  ii i ii i+* Re: So You Think You Can Const?36Julio Di Egidio
10 Jan 25 i  ii i ii ii+- Re: So You Think You Can Const?1Julio Di Egidio
13 Jan 25 i  ii i ii ii`* Re: So You Think You Can Const?34James Kuyper
13 Jan 25 i  ii i ii ii +* Re: So You Think You Can Const?32David Brown
13 Jan 25 i  ii i ii ii i+* Re: So You Think You Can Const?29Julio Di Egidio
13 Jan 25 i  ii i ii ii ii`* Re: So You Think You Can Const?28David Brown
13 Jan 25 i  ii i ii ii ii +* Re: So You Think You Can Const?26Julio Di Egidio
13 Jan 25 i  ii i ii ii ii i`* Re: So You Think You Can Const?25David Brown
14 Jan 25 i  ii i ii ii ii i +* Re: So You Think You Can Const?22Julio Di Egidio
14 Jan 25 i  ii i ii ii ii i i+* Re: So You Think You Can Const?16bart
14 Jan 25 i  ii i ii ii ii i ii`* Re: So You Think You Can Const?15David Brown
14 Jan 25 i  ii i ii ii ii i ii +* Re: So You Think You Can Const?7James Kuyper
14 Jan 25 i  ii i ii ii ii i ii i+- Re: So You Think You Can Const?1Dan Cross
14 Jan 25 i  ii i ii ii ii i ii i`* Re: So You Think You Can Const?5David Brown
14 Jan 25 i  ii i ii ii ii i ii i `* Re: So You Think You Can Const?4James Kuyper
15 Jan 25 i  ii i ii ii ii i ii i  `* Re: So You Think You Can Const?3James Kuyper
15 Jan 25 i  ii i ii ii ii i ii i   `* Re: So You Think You Can Const?2David Brown
15 Jan 25 i  ii i ii ii ii i ii i    `- Re: So You Think You Can Const?1Julio Di Egidio
14 Jan 25 i  ii i ii ii ii i ii +* Re: So You Think You Can Const?2Chris M. Thomasson
14 Jan 25 i  ii i ii ii ii i ii i`- Re: So You Think You Can Const?1Chris M. Thomasson
15 Jan 25 i  ii i ii ii ii i ii `* Re: So You Think You Can Const?5Julio Di Egidio
15 Jan 25 i  ii i ii ii ii i ii  +- Re: So You Think You Can Const?1Julio Di Egidio
15 Jan 25 i  ii i ii ii ii i ii  `* Re: So You Think You Can Const?3Kaz Kylheku
15 Jan 25 i  ii i ii ii ii i ii   `* Re: So You Think You Can Const?2Julio Di Egidio
15 Jan 25 i  ii i ii ii ii i ii    `- Re: So You Think You Can Const?1Julio Di Egidio
14 Jan 25 i  ii i ii ii ii i i+- Re: So You Think You Can Const?1James Kuyper
14 Jan 25 i  ii i ii ii ii i i`* Re: So You Think You Can Const?4Keith Thompson
15 Jan 25 i  ii i ii ii ii i i `* Re: So You Think You Can Const?3Kenny McCormack
15 Jan 25 i  ii i ii ii ii i i  `* Re: So You Think You Can Const?2Julio Di Egidio
15 Jan 25 i  ii i ii ii ii i i   `- Re: So You Think You Can Const?1Kaz Kylheku
15 Jan 25 i  ii i ii ii ii i `* Re: So You Think You Can Const?2Julio Di Egidio
15 Jan 25 i  ii i ii ii ii i  `- Re: So You Think You Can Const?1Kaz Kylheku
15 Jan 25 i  ii i ii ii ii `- Re: So You Think You Can Const?1Julio Di Egidio
15 Jan 25 i  ii i ii ii i`* Re: So You Think You Can Const?2Julio Di Egidio
15 Jan 25 i  ii i ii ii i `- Re: So You Think You Can Const?1Julio Di Egidio
15 Jan 25 i  ii i ii ii `- Re: So You Think You Can Const?1Julio Di Egidio
13 Jan 25 i  ii i ii i`- Re: So You Think You Can Const?1Tim Rentsch
15 Jan 25 i  ii i ii `* Re: So You Think You Can Const?4Julio Di Egidio
18 Jan 25 i  ii i ii  `* Re: So You Think You Can Const?3Tim Rentsch
19 Jan 25 i  ii i ii   `* Re: So You Think You Can Const?2Julio Di Egidio
20 Jan 25 i  ii i ii    `- Re: So You Think You Can Const?1Chris M. Thomasson
10 Jan 25 i  ii i i`- Re: So You Think You Can Const?1Ben Bacarisse
12 Jan 25 i  ii i +* Re: So You Think You Can Const?5Waldek Hebisch
12 Jan 25 i  ii i i+* Re: So You Think You Can Const?3Julio Di Egidio
13 Jan 25 i  ii i ii`* Re: So You Think You Can Const?2David Brown
13 Jan 25 i  ii i ii `- Re: So You Think You Can Const?1James Kuyper
13 Jan 25 i  ii i i`- Re: So You Think You Can Const?1David Brown
15 Jan 25 i  ii i `- Re: So You Think You Can Const?1Julio Di Egidio
15 Jan 25 i  ii +- Re: So You Think You Can Const?1Julio Di Egidio
15 Jan 25 i  ii `* Re: So You Think You Can Const?2Julio Di Egidio
15 Jan 25 i  ii  `- Re: So You Think You Can Const?1Chris M. Thomasson
8 Jan 25 i  i+* Re: So You Think You Can Const?2James Kuyper
8 Jan 25 i  ii`- Re: So You Think You Can Const?1James Kuyper
8 Jan 25 i  i+* Re: So You Think You Can Const?2Tim Rentsch
9 Jan 25 i  ii`- Re: So You Think You Can Const?1Julio Di Egidio
8 Jan 25 i  i+- Re: So You Think You Can Const?1Chris M. Thomasson
9 Jan 25 i  i`* Re: So You Think You Can Const?2Ben Bacarisse
9 Jan 25 i  i `- Re: So You Think You Can Const?1Julio Di Egidio
8 Jan 25 i  +* Re: So You Think You Can Const?2Tim Rentsch
9 Jan 25 i  i`- Re: So You Think You Can Const?1Ben Bacarisse
15 Jan 25 i  `- Re: So You Think You Can Const?1Julio Di Egidio
8 Jan 25 +* Re: So You Think You Can Const?35David Brown
8 Jan 25 i+* Re: So You Think You Can Const?4Ben Bacarisse
8 Jan 25 ii`* Re: So You Think You Can Const?3David Brown
15 Jan 25 ii `* Re: So You Think You Can Const?2Julio Di Egidio
15 Jan 25 ii  `- Re: So You Think You Can Const?1Julio Di Egidio
8 Jan 25 i+* What is wrong with malloc? (Was: So You Think You Can Const?)28Julio Di Egidio
8 Jan 25 ii+* Re: What is wrong with malloc? (Was: So You Think You Can Const?)25David Brown
8 Jan 25 iii+* Re: What is wrong with malloc? (Was: So You Think You Can Const?)2Julio Di Egidio
8 Jan 25 iiii`- Re: What is wrong with malloc? (Was: So You Think You Can Const?)1David Brown
8 Jan 25 iii+* Re: What is wrong with malloc? (Was: So You Think You Can Const?)13Phillip
8 Jan 25 iiii+- Re: What is wrong with malloc? (Was: So You Think You Can Const?)1Tim Rentsch
8 Jan 25 iiii+* Re: What is wrong with malloc?10Keith Thompson
8 Jan 25 iiiii`* Re: What is wrong with malloc?9Phillip
8 Jan 25 iiiii `* Re: What is wrong with malloc?8Keith Thompson
9 Jan 25 iiiii  `* Re: What is wrong with malloc?7Phillip
9 Jan 25 iiiii   +* Re: What is wrong with malloc?4Keith Thompson
9 Jan 25 iiiii   i+- Re: What is wrong with malloc?1Phillip
9 Jan 25 iiiii   i`* Re: What is wrong with malloc?2Michael S
9 Jan 25 iiiii   i `- Re: What is wrong with malloc?1Phillip
9 Jan 25 iiiii   `* Re: What is wrong with malloc?2David Brown
9 Jan 25 iiii`- Re: What is wrong with malloc? (Was: So You Think You Can Const?)1David Brown
8 Jan 25 iii+* Re: What is wrong with malloc?3Keith Thompson
9 Jan 25 iii`* Re: What is wrong with malloc? (Was: So You Think You Can Const?)6Julio Di Egidio
8 Jan 25 ii`* Re: What is wrong with malloc? (Was: So You Think You Can Const?)2James Kuyper
8 Jan 25 i+- Re: So You Think You Can Const?1Kaz Kylheku
8 Jan 25 i`- Re: So You Think You Can Const?1Keith Thompson
8 Jan 25 +* Re: So You Think You Can Const?51Andrey Tarasevich
8 Jan 25 +- Re: So You Think You Can Const?1Tim Rentsch
13 Jan 25 +- Re: So You Think You Can Const?1Chris M. Thomasson
15 Jan 25 +* Re: So You Think You Can Const?4Lawrence D'Oliveiro
26 Jan 25 `- Re: So You Think You Can Const?1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal