Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #86631

Re: How do C -programmers do reliable string handling?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c++
Subject Re: How do C -programmers do reliable string handling?
Date 2022-09-26 17:54 -0700
Organization None to speak of
Message-ID <87ill9mybn.fsf@nosuchdomain.example.com> (permalink)
References (5 earlier) <_55YK.371274$SAT4.327548@fx13.iad> <tgr9mp$3logk$2@dont-email.me> <tgsfth$3le$1@gioia.aioe.org> <tgsseu$3q78r$1@dont-email.me> <KFrYK.918314$%fx6.126666@fx14.ams1>

Show all headers | View raw


Richard Damon <Richard@Damon-Family.org> writes:
> On 9/26/22 2:53 PM, JiiPee wrote:
>> On 26/09/2022 18:19, Muttley@dastardlyhq.com wrote:
>>> Its a bit hard to accidentaly write &(str->length) rather than
>>> str->length
>> no, I mean if the function takes a pointer (not a variable), and we
>> are meant to pass a pointer to the lenght. But we dont know its also 
>> changing it.
>> Lets *assume* that a calculation is done like this:
>> float calculate(int round, int* lenght);
>> And then you call it with your lenght:
>> calculate(1110, &(str->length));
>> thinking that its only reading the lenght. but its changing it also.
>
> In C, a function taking a pointer is likely going to change it, why
> else use a pointer.
>
> This makes the intent fairly clear.

And if the function doesn't intend to change it, it should be a
pointer-to-const (which makes good sense if copying the value would be
unreasonably expensive or would have side effects).

> C++, allowing functions have non-const reference parameters means it
> is harder to tell by looking at a call site, what are outputs.

And again, you can use a const reference if you don't want to change the
value.  If a parameter is a non-const pointer (in C or C++) or a
non-const reference (in C++), you should assume that the referenced
object may be modified.  If it isn't, complain to the person who's
responsible for the function.

> This is handy at times, but means you need to think of things differently.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How do C -programmers do reliable string handling? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-09-24 20:22 +0300
  Re: How do C -programmers do reliable string handling? Bo Persson <bo@bo-persson.se> - 2022-09-24 21:00 +0200
  Re: How do C -programmers do reliable string handling? Gawr Gura <gawrgura@mail.hololive.com> - 2022-09-24 12:29 -0700
    Re: How do C -programmers do reliable string handling? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-09-25 00:47 +0300
      Re: How do C -programmers do reliable string handling? Muttley@dastardlyhq.com - 2022-09-25 08:50 +0000
        Re: How do C -programmers do reliable string handling? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-09-25 13:37 +0300
        Re: How do C -programmers do reliable string handling? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-09-25 13:38 +0300
        Re: How do C -programmers do reliable string handling? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-25 13:34 +0200
          Re: How do C -programmers do reliable string handling? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-25 17:00 +0200
          Re: How do C -programmers do reliable string handling? Muttley@dastardlyhq.com - 2022-09-25 15:14 +0000
      Re: How do C -programmers do reliable string handling? Gawr Gura <gawrgura@mail.hololive.com> - 2022-09-25 09:22 -0700
        Re: How do C -programmers do reliable string handling? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-09-26 01:01 +0300
          Re: How do C -programmers do reliable string handling? Richard Damon <Richard@Damon-Family.org> - 2022-09-25 19:02 -0400
            Re: How do C -programmers do reliable string handling? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-09-26 07:27 +0300
              Re: How do C -programmers do reliable string handling? Muttley@dastardlyhq.com - 2022-09-26 15:19 +0000
                Re: How do C -programmers do reliable string handling? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-09-26 21:53 +0300
                Re: How do C -programmers do reliable string handling? Richard Damon <Richard@Damon-Family.org> - 2022-09-26 20:42 -0400
                Re: How do C -programmers do reliable string handling? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-09-26 17:54 -0700
                Re: How do C -programmers do reliable string handling? Muttley@dastardlyhq.com - 2022-09-27 09:36 +0000
                Re: How do C -programmers do reliable string handling? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-09-27 15:28 +0100
          Re: How do C -programmers do reliable string handling? Gawr Gura <gawrgura@mail.hololive.com> - 2022-09-25 16:34 -0700
            Re: How do C -programmers do reliable string handling? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-09-26 07:32 +0300
    Re: How do C -programmers do reliable string handling? Juha Nieminen <nospam@thanks.invalid> - 2022-09-26 08:33 +0000
      Re: How do C -programmers do reliable string handling? Paavo Helde <eesnimi@osa.pri.ee> - 2022-09-26 12:18 +0300
        Re: How do C -programmers do reliable string handling? Juha Nieminen <nospam@thanks.invalid> - 2022-09-26 09:47 +0000
      Re: How do C -programmers do reliable string handling? Gawr Gura <gawrgura@mail.hololive.com> - 2022-09-26 07:40 -0700
  Re: How do C -programmers do reliable string handling? Paavo Helde <eesnimi@osa.pri.ee> - 2022-09-24 23:04 +0300
    Re: How do C -programmers do reliable string handling? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-09-25 00:44 +0300
  Re: How do C -programmers do reliable string handling? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-09-26 02:03 -0700

csiph-web