May a string span multiple, independent objects?

Liste des GroupesRevenir à cs c 
Sujet : May a string span multiple, independent objects?
De : vincent-news (at) *nospam* vinc17.net (Vincent Lefevre)
Groupes : comp.std.c
Date : 03. Jul 2024, 15:31:27
Autres entêtes
Organisation : a training zoo
Message-ID : <20240703141500$00ed@vinc17.org>
User-Agent : tin/2.6.4-20240531 ("Banff") (Linux/6.7.12-amd64 (x86_64))
ISO C17 (and C23 draft) 7.1.1 defines a string as follows: "A string
is a contiguous sequence of characters terminated by and including
the first null character."

But may a string span multiple, independent objects that happens
to be contiguous in memory?

For instance, is the following program valid and what does the ISO C
standard say about that?

#include <stdio.h>
#include <string.h>

typedef char *volatile vp;

int main (void)
{
  char a = '\0', b = '\0';
  vp p = &a, q = &b;

  printf ("%p\n", (void *) p);
  printf ("%p\n", (void *) q);
  if (p + 1 == q)
    {
      a = 'x';
      printf ("%zd\n", strlen (p));
    }
  if (q + 1 == p)
    {
      b = 'x';
      printf ("%zd\n", strlen (q));
    }
  return 0;
}

If such a program is valid, would there be issues by working with
pointers on such a string, say, dereferencing p[1] in the first "if"
(which is normally UB)?

--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

Date Sujet#  Auteur
3 Jul 24 * May a string span multiple, independent objects?13Vincent Lefevre
3 Jul 24 +* Re: May a string span multiple, independent objects?4Hans-Bernhard Bröker
3 Jul 24 i+- Re: May a string span multiple, independent objects?1Vincent Lefevre
3 Jul 24 i+- Re: May a string span multiple, independent objects?1James Kuyper
8 Aug 24 i`- Re: May a string span multiple, independent objects?1Tim Rentsch
3 Jul 24 +* Re: May a string span multiple, independent objects?7James Kuyper
3 Jul 24 i`* Re: May a string span multiple, independent objects?6Ben Bacarisse
3 Jul 24 i +- Re: May a string span multiple, independent objects?1James Kuyper
4 Jul 24 i `* Re: May a string span multiple, independent objects?4Vincent Lefevre
5 Jul 24 i  +- Re: May a string span multiple, independent objects?1Ben Bacarisse
5 Jul 24 i  +- Re: May a string span multiple, independent objects?1James Kuyper
8 Aug 24 i  `- Re: May a string span multiple, independent objects?1Tim Rentsch
5 Jul 24 `- Re: May a string span multiple, independent objects?1Kaz Kylheku

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal