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


Groups > comp.lang.c > #69968

Re: A finalized program

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: A finalized program
Date 2015-09-08 21:00 +0100
Organization A noiseless patient Spider
Message-ID <874mj4of4e.fsf@bsb.me.uk> (permalink)
References <d93ca264-e41a-4f82-8c09-cd87731b2eed@googlegroups.com> <0b40a48e-3d88-46dd-adf2-275a33a87f65@googlegroups.com> <87r3m9nhwp.fsf@bsb.me.uk> <4371016b-309f-4f89-9b94-91088121f113@googlegroups.com>

Show all headers | View raw


Malcolm McLean <malcolm.mclean5@btinternet.com> writes:

> On Tuesday, September 8, 2015 at 2:46:10 PM UTC+1, Ben Bacarisse wrote:
>> 
>> But there is a problem with the way you use strlen.  It's a function
>> that returns an unsigned value -- not an int (specifically a value of
>> type size_t used in C to represent sizes of things). 
>>
> If you want another demonstration of the evils of size_t, here's some
> code I'm working on literally this minute.
>
> it's an undo / redo buffer with a "track" from the root, which was the
> document as created, to the current tip. There's also a "head" which
> shows where the next change will be inserted.  So it's very simple
> boilerplate code, unfortunately C++.
>
>  std::vector <Node *> getCurrentTrack(int *aHeadIndex) const
>     {
>         Node *node = mRoot;
>         std::vector <Node *> answer;
>         *aHeadIndex = -1;
>         while(node)
>         {
>             answer.push_back(node);
>             if(node == mHead)
>                 *aHeadIndex = (int) answer.size() -1;
>             node = node->next;
>         }
>         return answer;
>     }
>
> But if the index is a size_t, and the track is empty, what are we
> supposed to set aHeadIndex to?

But the index isn't a size_t -- someone (you?) chose int because you
want to use -1 as a special value.  If you want the index to be a size_t
the design has to change.  Set it to the final answer.size() (which is
guaranteed to be invalid) or, indeed, set it to (size_t)-1.  What value
denotes "no head index" is part of the contract with the caller so the
designer chooses.  You could even use the style of the standard
containers ans use an iterator.

> Or if the head isn't on the track
> (shouldn't happen, but maybe if the system is extended, you'll have
> "detached heads" as in git).

I don't follow this.

-- 
Ben.

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


Thread

A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 04:13 -0700
  Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 04:19 -0700
    Re: A finalized program fir <profesor.fir@gmail.com> - 2015-09-08 04:30 -0700
    Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 14:45 +0100
      Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 08:29 -0700
      Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 08:52 -0700
        Re: A finalized program Martin Shobe <martin.shobe@yahoo.com> - 2015-09-08 13:01 -0500
        Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 21:00 +0100
          Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 13:33 -0700
            Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-09 00:28 +0100
  Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 14:26 +0100
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 06:42 -0700
      Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 20:50 +0100
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 01:47 -0700
  Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-08 13:41 +0000
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 01:31 -0700
      Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-10 10:39 +0000
        Re: A finalized program David Thompson <dave.thompson2@verizon.net> - 2015-09-20 18:35 -0400
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 01:33 -0700
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 10:16 -0700
      Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-10 20:08 +0200
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:05 -0700
          Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:17 +0100
            Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:19 +0100
            Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:28 -0700
            Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:29 -0700
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:19 -0700
          Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:23 +0100
            Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:33 -0700
              Re: A finalized program Keith Thompson <kst-u@mib.org> - 2015-09-11 14:11 -0700
                Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 21:39 +0000
          Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 12:49 +0200
      Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 23:31 +0100
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:15 -0700
      Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-10 22:37 +0000
        Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 23:47 +0100
          Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 10:18 +0000
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 01:07 -0700
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:14 -0700
          Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 10:10 +0000
            Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:19 -0700
  Re: A finalized program Barry Schwarz <schwarzb@dqel.com> - 2015-09-08 15:53 -0700
  Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:27 -0700
    Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 14:05 +0200
  Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:42 -0700
    Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 14:15 +0200
      Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 05:50 -0700
        Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 15:00 +0200
      Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 05:50 -0700
    Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-11 13:46 +0100
      Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 14:19 +0100

csiph-web