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

Re: programming



On Mon, Nov 05, 2001 at 11:40:02PM -0500, dman wrote:
> On Mon, Nov 05, 2001 at 01:17:15PM -0600, Dimitri Maziuk wrote:
> | * J.A.Serralheiro (mrserra@ci.uc.pt) spake thusly:
> | > so, there is no way to know in advance the length of a string unless you
> | > have an identifier, or constant for it. 
> | 
> | Que? You can't know $FOO of $BAR until you take $BAR in and see what
> | sort of $FOO it has. You can't know the length of a string until you 
> | read it in and count the characters.
> 
> Right, but if you don't know ahead of time that the sequence of
> characters is NUL terminated, how do you know when to stop counting
> characters?

Use strnlen(). It's a GNU extension, but you can always take the
implementation of it from glibc if your code needs to be portable.

       The  strnlen  function returns the number of characters in
       the string pointed to by s, not including the  terminating
       '\0' character, but at most maxlen. In doing this, strnlen
       looks only at the first maxlen characters at s  and  never
       beyond s+maxlen.

> C is great of low-level, close-to-the-metal, sort of programming like
> kernels, device drivers, and interpreters.  However for application
> development, where high-level data structures, logic, and interactions
> are common a higher level language is much more appropriate and is
> likely to reduce development effeort required.

C is great if you know the C library well. ;)

My opinions on this vary depending on what language I'm using at the
moment, which probably indicates that I don't find it a big problem. I
write mostly C++ at work: great if you have decent libraries and are
careful, but if not you can come up with horribly bloated code without
even knowing it. I maintain some large C programs: with a little
discipline you can keep code pretty concise and readable, and abstract
most of the fiddly bits into easily maintainable libraries. If I'm
writing something new then chances are I'll use Perl, mainly because one
of the Perl virtues, laziness, is also one of mine.

-- 
Colin Watson                                  [cjwatson@flatline.org.uk]



Reply to: