Re: Malcolm's new book




"Kelsey Bjarnason" <kbjarnason@xxxxxxxxx> wrote in message news:aa3ao4-ej5.ln1@xxxxxxxxxxxxxxxxxxxxxxx
[snips]

Why would size_t be obsolete, since it does a job int does not do?

So? It exists now, it has reasons for existing now, and you are ignoring
them and as a result producing bad, broken, amateurish code.

size_t didn't use to be part of the language. It's a relatively new invention designed to solve the problem of memory buffers bigger than the range of an int. Of course such buffers should seldom arise, if you obey the convention that int is the "natural" integer size of the machine.

If we allow that any array can overflow the size of an int we get the folowing

void payroll(struct Employee *emplloyees, size_t N)
{
size_t i;

for(i=0;i<N;i++)
/* so something */
}

Now N is arguably a size, though it is a slightly confusing term. However the type of i is downright misleading. It does not hold a size at all. In fact this won't be acceptable, and something will be done. Either size_t will have to be deprecated / quietly dropped, or C itself will be regarded as an "advanced" language unsuitable for beginners, and gradually be squeezed out.
I'm hoping for 1.
size_t is a kludge, and code is clearer without it. It has no place in my book, except in the introduction where I explain the conventions used, and why the code is written as it is - to show algorithm, not to be robust to extreme inputs as you would get in production code. Hence the memory allocation codes take int because the algorithms operate on integers.

If you disagree with this choice, I include an example of "real" code, an atan2 routine form Sun, so that the reader has some exposure to what production code can look like.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

.



Relevant Pages

  • Re: Variable declaration - Is this valid in standard C?
    ... do this in production code. ... int main ... at 3:00am my mind can play some wicked tricks on me--better to be safe ... and use a Standard C function like getsand give it enough fuel:) ...
    (comp.lang.c)
  • Re: byte order
    ... int am_i_little_endian{ ... Are you *absolutely* sure that's standard C, ... guarantee, though. ... production code and on numerous platforms. ...
    (comp.lang.lisp)
  • Re: byte order
    ... of the underlying 'machine'? ... int am_i_little_endian{ ... Are you *absolutely* sure that's standard C, ... production code and on numerous platforms. ...
    (comp.lang.lisp)
  • Re: size_t problems
    ... If size_t is smaller than int, ... In writing the code, I thought it so strange for size_t to be anything ... I also consider such an implementation very strange and probably wouldn't ... care about in production code (although I might add an assertion to ...
    (comp.lang.c)
  • Re: C++ more efficient than C?
    ... If you want to do language comparisons, ... complicated algorithm, though). ... int compare ... not been in C's standard library. ...
    (comp.lang.c)