Re: What is the reason for Perl?



John Bokma <john@xxxxxxxxxxxxxxx> wrote in
news:Xns972AA09354FA1castleamber@xxxxxxxxxxx:

> "Eric J. Roode" <sdn.girths00869@xxxxxxxxxxx> wrote:
>
>> For example: You're writing a library function, which writes to a
>> buffer provided by the caller. The caller will typically allocate a
>> string (say), either on the stack or dynamically via malloc, and pass
>> it to your function. Wouldn't it be nice if your function could
>> determine how large the buffer was,
>
> If it's a string, you could check for \0 now couldn't you? And if you
> want to use something else, what's the problem of wrapping it all nice
> into a library?

char buf[80];
strcpy (buf, "hello");

After this call, buf[5] is '\0'. Before that call, all of the elements of
buf are uninitialized; possibly they're all '\0', possibly they're all
random garbage.

Checking for '\0' tells you how long the string is (5 characters), but
doesn't tell you how long the allocated buffer is (80 bytes).

char buf[4];
strcpy (buf, "hello");

strcpy() can't tell that you're trying to stuff too much data into the
buffer. There simply is no way.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
.



Relevant Pages

  • Re: Why crash ?
    ... Yes, but naively, I would assume that the optimal buffer size ... depended on a certain number of characters, ... small string optimization is that the std::string object has a certain ... If you allocate dynamic storage, the string object has to store ...
    (microsoft.public.vc.language)
  • Re: Why crash ?
    ... Yes, but naively, I would assume that the optimal buffer size ... depended on a certain number of characters, ... small string optimization is that the std::string object has a certain ... If you allocate dynamic storage, the string object has to store ...
    (microsoft.public.vc.language)
  • Re: writing alibrary funtion ssprintf()
    ... length of the string and then again malloc(not exactly malloc ....there ... Snprintf will return the size of the buffer required for printing the whole string, so you can always adjust the default size upwards to match that number, and keep it that way in subsequent calls. ... You can thus take uio.uio_resid and use it to allocate the string's buffer, before flushing the buffer to it. ...
    (comp.lang.c)
  • Re: How to take in a string of any size?
    ... >>The idea is that if the string becomes as large as the buffer, ... >>the buffer size is probably a good bet. ... > memory than realloc() can allocate - and there is no check to be ...
    (comp.lang.c)
  • Re: _itoa( )
    ... Does char *_itoa(int value, char *string, int radix) allocate the buffer ... a pointer to char is passed by value, therefore the caller _must_ allocate ...
    (alt.comp.lang.learn.c-cpp)