Re: Is the size_t return type of strlen() a design flaw?

Discussion in 'C++' started by James Kanze, Feb 20, 2011.

  1. James Kanze

    James Kanze Guest

    On Feb 19, 3:33 pm, Paavo Helde <> wrote:
    > Leigh Johnston <> wrote in news:bKqdnWIWH6D8-
    > :


    [...]
    > > I only expect trolls to reply to this post.


    > Then your expectations have failed you, because "K4 Monk" does not seem
    > like a troll.


    Leigh has a somewhat unusual definition of troll: anyone who
    disagrees with Leigh. (According to the usual definition,
    Leigh's original posting in this thread is a troll.)

    > Returning size_t from strlen() is OK. However, typedeffing
    > size_t to an unsigned type was a design flaw IMO.


    I don't think you can discuss the question intelligently
    withou knowing why the C standards committee decided that size_t
    must be unsigned, and that strlen should return a size_t. (The
    original strlen, in Kernighan and Richie C, returned int.) Nor
    without being aware of the prevelance of 16 bit machines at the
    time. Like most decisions in the committee, it represented a
    compromise, based on the environments the members of the
    committee knew. Had 32 bit machines been the standard at the
    time, it's probable that the committee would have gone along
    with Kernighan and Richie, not defined a size_t, and used int
    every where. But that's all in the passed.

    > BTW, netiquette rules says it is not nice to put the question only in the
    > message subject.


    Netiquette says it's not nice to troll; i.e. to post a
    contentious message with the sole aim of generating a lot of
    useless traffic. (That's the classical definition of troll.)

    --
    James Kanze
    James Kanze, Feb 20, 2011
    #1
    1. Advertising

  2. James Kanze

    Öö Tiib Guest

    On Feb 20, 8:33 pm, Paavo Helde <> wrote:
    > James Kanze <> wrote innews::
    >
    > > On Feb 19, 3:33 pm, Paavo Helde <> wrote:
    > >> Returning size_t from strlen() is OK. However, typedeffing
    > >> size_t to an unsigned type was a design flaw IMO.

    >
    > > I don't think you can discuss the question intelligently
    > > withou knowing why the C standards committee decided that size_t
    > > must be unsigned, and that strlen should return a size_t.  (The
    > > original strlen, in Kernighan and Richie C, returned int.)  Nor
    > > without being aware of the prevelance of 16 bit machines at the
    > > time.  Like most decisions in the committee, it represented a
    > > compromise, based on the environments the members of the
    > > committee knew.  Had 32 bit machines been the standard at the
    > > time, it's probable that the committee would have gone along
    > > with Kernighan and Richie, not defined a size_t, and used int
    > > every where.  But that's all in the passed.

    >
    > That sounds like a design flaw to me - preferring a solution which solves
    > a short-term problem, but will cause problems forever later.
    >
    > They might have gone with the 'char' route: saying that the signedness of
    > size_t is implementation-defined, with a suggestion to use an unsigned
    > type in 16-bit and a signed type in 32-bit and higher.
    >
    > Ok, this is all most futile of course, so I will try to keep my mouth
    > shut about size_t from now on.


    :) Lets talk about short and long then and what these keywords
    guarantee by language description. It is hard to imagine that someone
    benefits from such strange "fundamental" types.
    Öö Tiib, Feb 20, 2011
    #2
    1. Advertising

  3. James Kanze

    Öö Tiib Guest

    On Feb 21, 9:08 am, Paavo Helde <> wrote:
    > Tiib <> wrote innews::
    >
    > >:) Lets talk about short and long then and what these keywords
    > > guarantee by language description. It is hard to imagine that someone
    > > benefits from such strange "fundamental" types.

    >
    > This is actually very relevant. There are only a couple of places in
    > standard requiring the use of 'n parts where Leigh would expect size_t


    > The problems with unspecified size of short and long have at least been
    > addressed by <stdint.h> for a decade already.


    I don't have problem with missing types with specified size. I have
    problem with
    Öö Tiib, Feb 21, 2011
    #3
  4. James Kanze

    Öö Tiib Guest

    Doh, discard my previous reply ... cat did jump onto keyboard and
    somehow post it.

    On Feb 21, 9:08 am, Paavo Helde <> wrote:
    > Tiib <> wrote innews::
    >
    > >:) Lets talk about short and long then and what these keywords
    > > guarantee by language description. It is hard to imagine that someone
    > > benefits from such strange "fundamental" types.

    >
    > This is actually very relevant. There are only a couple of places in
    > standard requiring the use of 'long', mostly (only?) in C-inherited
    > parts. For example,
    >
    > long ftell(FILE *stream);
    > int fseek(FILE *stream, long offset, int origin);
    >
    > The 'long' here plays effectively the same r le here than 'size_t' for
    > memory access. However, it is a signed type. One could have complained
    > that as file offset cannot be negative, ftell() should return an unsigned
    > type. However, it is immediately clear that the return value might be
    > used in arithmetic expressions and fed back to fseek() (which actually
    > supports negative seeks), so there would be no point of using an unsigned
    > type.


    Similarly if you take strlen you might want to compare it with
    something or to calculate difference. So long is used in places where
    Leigh would expect size_t.

    > The problems with unspecified size of short and long have at least been
    > addressed by <stdint.h> for a decade already.


    There is still problem of presence of long and short in language.
    These are like broken feature in program, few users may be still are
    using it for something, so it can not be removed, on other hand it is
    hopeless to fix it. So additional abomination "long long" is in the
    process of entering language instead.
    Öö Tiib, Feb 21, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin

    ASP.Net design flaw!?

    Kevin, Jan 22, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    905
    Kevin
    Jan 27, 2004
  2. Andrew
    Replies:
    0
    Views:
    452
    Andrew
    Jun 29, 2005
  3. =?ISO-8859-1?Q?Christian_Engstr=F6m?=

    non-const function return values: gcc bug or language flaw?

    =?ISO-8859-1?Q?Christian_Engstr=F6m?=, Feb 7, 2004, in forum: C++
    Replies:
    19
    Views:
    1,131
    tom_usenet
    Feb 10, 2004
  4. Alex Vinokur
    Replies:
    9
    Views:
    773
    James Kanze
    Oct 13, 2008
  5. Alex Vinokur
    Replies:
    1
    Views:
    566
Loading...

Share This Page