Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?

Liste des GroupesRevenir à cl c 
Sujet : Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?
De : ben (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.lang.c
Date : 06. Aug 2024, 13:29:29
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <875xsdc2jq.fsf@bsb.me.uk>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
User-Agent : Gnus/5.13 (Gnus v5.13)
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:

I must have completely missed it. Sorry about that. Please redefine?

It's going to seem silly after all these exchanges.  I simply wanted to
know why you chose to use const as you originally posted:

| struct object_prv_vtable {
|       int (*fp_destroy) (void* const);
|       int (*fp_read) (void* const, void*, size_t);
|       int (*fp_write) (void* const, void const*, size_t);
| };

because that looks peculiar (to the point of being arbitrary) to me.
You went on to talk about "self" pointers being const pointers to const
void, but that was not what you wrote, so it did not address what I was
asking about.

In general, const qualified argument types are rarely used and are even
more rarely used in function (or type) declarations because there have
no effect at all in that position.  For example, I can assign fp_destroy
from a function declared without the const-qualified parameter:

   int destroy(void *self) { /* ... */; return 1; }
   ...
   vtab.fp_destroy = destroy;

or, if I do want the compiler to check that the function does not alter
its parameter, I can add the const in the function definition (were it
can be useful) even if it is missing from the declaration:

  struct object_prv_vtable {
        int (*fp_destroy) (void*);
        /* ... */
  };

  int destroy(void *const self) { /* ... */; return 1; }
  ...
  vtab.fp_destroy = destroy;

But if you want the const there so that the declaration matches the
function defintion, why not do that for all the parameters?  Basically,
I would have expercted either this (just ine const where it matters):

struct object_prv_vtable {
      int (*fp_destroy) (void *);
      int (*fp_read) (void *, void *, size_t);
      int (*fp_write) (void *, void const *, size_t);
};

and the actual functions that get assigned to these pointers might, if
you want that extra check, have all their parametera marked const.  Or,
for consistency, you might have written

struct object_prv_vtable {
      int (*fp_destroy) (void * const);
      int (*fp_read) (void * const, void * const, size_t const);
      int (*fp_write) (void * const, void const * const, size_t const);
};

even if none of the actual functions have const parameters.

Finally, if you had intended to write what you later went on to talk
about, you would have written either

struct object_prv_vtable {
      int (*fp_destroy) (const void *);
      int (*fp_read) (const void *, void *, size_t);
      int (*fp_write) (const void *, void const *, size_t);
};

or

struct object_prv_vtable {
      int (*fp_destroy) (const void * const);
      int (*fp_read) (const void * const, void * const, size_t const);
      int (*fp_write) (const void * const, void const * const, size_t const);
};

TL;DR: where you put the consts in the original just seemed arbitrary.


I'll also note that the term "const pointer" is often used when the
pointer is not const!  It most often mean that the pointed-to type is
const qualified.  As such, it's best to avoid the term altogether.

--
Ben.

Date Sujet#  Auteur
1 Aug 24 * relearning C: why does an in-place change to a char* segfault?98Mark Summerfield
1 Aug 24 +* Re: relearning C: why does an in-place change to a char* segfault?2Mark Summerfield
1 Aug 24 i`- Re: relearning C: why does an in-place change to a char* segfault?1Ben Bacarisse
1 Aug 24 +* Re: relearning C: why does an in-place change to a char* segfault?33Richard Harnden
1 Aug 24 i+- Re: relearning C: why does an in-place change to a char* segfault?1Mark Summerfield
1 Aug 24 i`* Re: relearning C: why does an in-place change to a char* segfault?31Bart
1 Aug 24 i `* Re: relearning C: why does an in-place change to a char* segfault?30Keith Thompson
1 Aug 24 i  +* Re: relearning C: why does an in-place change to a char* segfault?20Bart
1 Aug 24 i  i+- Re: relearning C: why does an in-place change to a char* segfault?1Keith Thompson
2 Aug 24 i  i+- Re: relearning C: why does an in-place change to a char* segfault?1James Kuyper
2 Aug 24 i  i+* Re: relearning C: why does an in-place change to a char* segfault?16Kaz Kylheku
2 Aug 24 i  ii`* Re: relearning C: why does an in-place change to a char* segfault?15Bart
2 Aug 24 i  ii +- Re: relearning C: why does an in-place change to a char* segfault?1Richard Damon
2 Aug 24 i  ii `* Re: relearning C: why does an in-place change to a char* segfault?13James Kuyper
2 Aug 24 i  ii  +- Re: relearning C: why does an in-place change to a char* segfault?1Bart
3 Aug 24 i  ii  +* Re: relearning C: why does an in-place change to a char* segfault?5Lawrence D'Oliveiro
3 Aug 24 i  ii  i`* Re: relearning C: why does an in-place change to a char* segfault?4Richard Damon
3 Aug 24 i  ii  i +- Re: relearning C: why does an in-place change to a char* segfault?1Joe Pfeiffer
4 Aug 24 i  ii  i +- Re: relearning C: why does an in-place change to a char* segfault?1Lawrence D'Oliveiro
12 Aug 24 i  ii  i `- Re: relearning C: why does an in-place change to a char* segfault?1Tim Rentsch
14 Aug 24 i  ii  `* Re: relearning C: why does an in-place change to a char* segfault?6Tim Rentsch
14 Aug 24 i  ii   +* Re: relearning C: why does an in-place change to a char* segfault?3Keith Thompson
16 Aug 24 i  ii   i`* Re: relearning C: why does an in-place change to a char* segfault?2Tim Rentsch
16 Aug 24 i  ii   i `- Re: relearning C: why does an in-place change to a char* segfault?1Keith Thompson
14 Aug 24 i  ii   `* Re: relearning C: why does an in-place change to a char* segfault?2James Kuyper
16 Aug 24 i  ii    `- Re: relearning C: why does an in-place change to a char* segfault?1Tim Rentsch
4 Aug 24 i  i`- Re: relearning C: why does an in-place change to a char* segfault?1Bonita Montero
12 Aug 24 i  `* Re: relearning C: why does an in-place change to a char* segfault?9Tim Rentsch
13 Aug 24 i   `* Re: relearning C: why does an in-place change to a char* segfault?8Vir Campestris
13 Aug 24 i    +* Re: relearning C: why does an in-place change to a char* segfault?3Keith Thompson
14 Aug 24 i    i+- Re: relearning C: why does an in-place change to a char* segfault?1Tim Rentsch
14 Aug 24 i    i`- Re: relearning C: why does an in-place change to a char* segfault?1David Brown
14 Aug 24 i    `* Re: relearning C: why does an in-place change to a char* segfault?4Tim Rentsch
14 Aug 24 i     `* Re: relearning C: why does an in-place change to a char* segfault?3Keith Thompson
14 Aug 24 i      `* Re: relearning C: why does an in-place change to a char* segfault?2Kaz Kylheku
14 Aug 24 i       `- Re: relearning C: why does an in-place change to a char* segfault?1Keith Thompson
1 Aug 24 +* No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?38Michael S
1 Aug 24 i`* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?37David Brown
2 Aug 24 i `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?36candycanearter07
2 Aug 24 i  +* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?34Keith Thompson
2 Aug 24 i  i+* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?28Richard Harnden
2 Aug 24 i  ii+- Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?1James Kuyper
2 Aug 24 i  ii+* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?24Keith Thompson
2 Aug 24 i  iii+* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?5Richard Damon
2 Aug 24 i  iiii+* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?3James Kuyper
2 Aug 24 i  iiiii`* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?2Richard Damon
12 Aug 24 i  iiiii `- Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?1Tim Rentsch
12 Aug 24 i  iiii`- Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?1Tim Rentsch
2 Aug 24 i  iii+* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?15Chris M. Thomasson
3 Aug 24 i  iiii`* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?14Ben Bacarisse
3 Aug 24 i  iiii `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?13Chris M. Thomasson
5 Aug 24 i  iiii  `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?12Ben Bacarisse
5 Aug 24 i  iiii   `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?11Chris M. Thomasson
5 Aug 24 i  iiii    +- Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?1Chris M. Thomasson
5 Aug 24 i  iiii    `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?9Ben Bacarisse
5 Aug 24 i  iiii     `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?8Chris M. Thomasson
5 Aug 24 i  iiii      `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?7Ben Bacarisse
6 Aug 24 i  iiii       +* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?5Chris M. Thomasson
6 Aug 24 i  iiii       i`* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?4Ben Bacarisse
6 Aug 24 i  iiii       i `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?3Chris M. Thomasson
7 Aug 24 i  iiii       i  `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?2Ben Bacarisse
13 Aug 24 i  iiii       i   `- Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?1Chris M. Thomasson
6 Aug 24 i  iiii       `- Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?1Chris M. Thomasson
12 Aug 24 i  iii`* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?3Tim Rentsch
12 Aug 24 i  iii `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?2Keith Thompson
3 Sep 24 i  iii  `- Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?1Tim Rentsch
25 Aug 24 i  ii`* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?2dave thompson 2
25 Aug 24 i  ii `- Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?1Keith Thompson
12 Aug 24 i  i`* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?5Tim Rentsch
12 Aug 24 i  i `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?4Keith Thompson
13 Aug 24 i  i  `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?3Tim Rentsch
13 Aug 24 i  i   `* Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?2David Brown
13 Aug 24 i  i    `- Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?1Keith Thompson
3 Aug 24 i  `- Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?1David Brown
1 Aug 24 +- Re: relearning C: why does an in-place change to a char* segfault?1James Kuyper
1 Aug 24 `* Re: relearning C: why does an in-place change to a char* segfault?23Kaz Kylheku
1 Aug 24  +* Re: relearning C: why does an in-place change to a char* segfault?20Bart
1 Aug 24  i+- Re: relearning C: why does an in-place change to a char* segfault?1Keith Thompson
1 Aug 24  i+- Re: relearning C: why does an in-place change to a char* segfault?1Ben Bacarisse
2 Aug 24  i+* Re: relearning C: why does an in-place change to a char* segfault?3Kaz Kylheku
2 Aug 24  ii+- Re: relearning C: why does an in-place change to a char* segfault?1Bart
12 Aug 24  ii`- Re: relearning C: why does an in-place change to a char* segfault?1Tim Rentsch
3 Aug 24  i`* Re: relearning C: why does an in-place change to a char* segfault?14David Brown
4 Aug 24  i +* Re: relearning C: why does an in-place change to a char* segfault?12Keith Thompson
4 Aug 24  i i+* Re: relearning C: why does an in-place change to a char* segfault?10Lawrence D'Oliveiro
4 Aug 24  i ii`* Re: relearning C: why does an in-place change to a char* segfault?9Keith Thompson
4 Aug 24  i ii +* Re: relearning C: why does an in-place change to a char* segfault?2Richard Damon
12 Aug 24  i ii i`- Re: relearning C: why does an in-place change to a char* segfault?1Tim Rentsch
5 Aug 24  i ii `* Re: relearning C: why does an in-place change to a char* segfault?6Lawrence D'Oliveiro
5 Aug 24  i ii  `* Re: relearning C: why does an in-place change to a char* segfault?5Keith Thompson
5 Aug 24  i ii   `* Re: relearning C: why does an in-place change to a char* segfault?4Lawrence D'Oliveiro
6 Aug 24  i ii    `* Re: relearning C: why does an in-place change to a char* segfault?3Keith Thompson
6 Aug 24  i ii     `* Re: relearning C: why does an in-place change to a char* segfault?2Bart
6 Aug 24  i ii      `- Re: relearning C: why does an in-place change to a char* segfault?1David Brown
4 Aug 24  i i`- Re: relearning C: why does an in-place change to a char* segfault?1David Brown
4 Aug 24  i `- Re: relearning C: why does an in-place change to a char* segfault?1Keith Thompson
1 Aug 24  +- Re: relearning C: why does an in-place change to a char* segfault?1Keith Thompson
14 Aug 24  `- Re: relearning C: why does an in-place change to a char* segfault?1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal