Re: Malcolm's new book



[snips]

On Fri, 03 Aug 2007 07:18:13 +0100, Malcolm McLean wrote:

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.

How about obeying the convention that C defines its rules and they're
defined for good reasons, so one should pay attention to them?


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.

Actually, it's a count, but either way.

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.

Huh? You're not using it as a size, so something isn't acceptable?

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.

A language is a tool and a tool takes practice to master. If C is too
hard, you should stick to BASIC.

size_t is a kludge, and code is clearer without it.

The code in the example chapters is neither clearer nor good; it is broken
by design, because the author doesn't grasp basic concepts.

Simple example: using size_t to specify the size of the buffer to compress
makes sense; the buffer size will be 0..N. Using int makes it *less*
clear:: the developer obviously intended to allow negative values
specifying the length of the buffer; if he hadn't, he wouldn't be using a
plain int, he'd be using the proper type: size_t. Since he *is* expecting
negative values, the actual intent of the function is no longer clear, as
you cannot compress (or even create) a negatively-sized buffer, so there
must be a special meaning to such negative values, so let's haul out the
reference manual to see what the heck the code does in such cases.
Meanwhile we now have to also worry that our buffer might be too big and
overflow the length parameter, despite there being not a single good
reason for such a case to ever exist with properly-written code that uses
the proper types.

Yeah, sure makes it clearer.

It has no place in
my book,

"It" being "good coding practice".

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.

I assume it's ugly. All I can say is I'll take ugly code that works over
pretty code that's broken, any day.
.



Relevant Pages

  • [PATCH] Numerous fixes to kernel-doc info in source files.
    ... static inline int ffs ... @buffer: where the data must be copied. ... * struct kfifo with kfree. ... @timer: ...
    (Linux-Kernel)
  • [UNIX] Multiple Vulnerabilities in Citadel/UX
    ... could allow complete control over a vulnerable server. ... Citadel server as can be seen by this simplistic code snippet: ... configuration buffers, leading to the possibility of carrying out a buffer ... int connect_to_host; ...
    (Securiteam)
  • Re: pushing the envelope with sockets
    ... receiving on the socket they are received (upto the buffer size), you can even change what happens if the buffer runs full. ... int read = S.EndReceive; ... class AsyncReader: Reader ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] Updated: Reworked Cell OProfile: SPU mutex lock fix
    ... arch-independent pieces of the OProfile kernel driver that this patch ... kernel driver patches. ... into the kernel buffer without holding the buffer_mutex lock. ... int spu_sync_start; ...
    (Linux-Kernel)
  • [PATCH] char: make functions static in synclinkmp.c
    ... int *eof, void *data) ... /* Allocate DMA buffers for the transmit and receive descriptor lists. ... * This buffer is used to pass an assembled frame to the line discipline. ...
    (Linux-Kernel)