Re: [RFC] Convert builin-mailinfo.c to use The Better String Library.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Walter Bright <boost@xxxxxxxxxxxxxxx> writes:

> A canonical example is that of a loop. Consider a simple C loop over
> an array:
>
> void foo(int array[10])
> {
>     for (int i = 0; i < 10; i++)
>     {   int value = array[i];
>         ... do something ...
>     }
> }
>
> It's simple, but it has a lot of problems:
>
> 1) i should be size_t, not int

Wrong.  size_t is for holding the size of memory objects in bytes, not
in terms of indices.  For indices, the best variable is of the same
type as the declared index maximum size, so here it is typeof(10),
namely int.

> 2) array is not checked for overflow

Why should it?

> 3) 10 may not be the actual array dimension

Your point is?

> 4) may be more efficient to step through the array with pointers,
> rather than indices

No.  It is a beginners' and advanced users' mistake to think using
pointers for access is a good idea.  Trivial optimizations are what a
compiler is best at, not the user.  Using pointer manipulation will
more often than not break loop unrolling, loop reversal, strength
reduction and other things.

> 5) type of array may change, but the type of value may not get
> updated

Huh?

> 6) crashes if array is NULL

Certainly.  Your point being?

> 7) only works with arrays and pointers

Since there are only arrays and pointers in C, not really a restriction.

>
> Since this thread is talking about C++, let's look at the C++ version:
>
> void foo(std::vector<int> array)
> {
>   for (std::vector<int>::const_iterator
>        i = array.begin();
>        i != array.end();
>        i++)
>   {
>     int value = *i;
>     ... do something ...
>   }
> }

Where is my barf bag?

> Frankly, I don't want to write loops that way. I want to write them
> like this:
>
> void foo(int[] array)
> {
>   foreach (value; array)
>   {
>     ... do something ...
>   }
> }
>
> As a programmer, I'm specifying exactly what I want to happen without
> much extra puffery. It's less typing, simpler, and more resistant to
> bugs.
>
> 1) correct loop index type is selected based on the type of array
> 2) arrays carry with them their dimension, so foreach is guaranteed to
> step through the loop the correct number of times
> 3) implementation decides if pointers will do a better job than
> indices, based on the compilation target
> 4) type of value is inferred automatically from the type of array, so
> no worries if the type changes
> 5) Null arrays have 0 length, so no crashing
> 6) works with any collection type

Most of those are toy concerns.  They prevent problems that don't
actually occur much in practice.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum
-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux