|
From: Ioannis V. <no...@ya...> - 2001-10-19 15:20:38
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Jason Hardman
> Sent: Friday, October 19, 2001 4:40 PM
> To: Dev-C++
> Subject: Re: [Dev-C++] Errorhandling
>=20
>=20
> I can say for certain that strcat() won't ever include any=20
> extra '\0' into a string. If it did it would be due to a bug=20
> in the routine. The function is passed a pointer to the=20
> beginning of two strings, so the only way it could start at=20
> the end of a string would be to look for it. The only way it=20
You can't know it. strcat() can be written even in assembly and only the
author's mind knows how it works. Simply C and C++ guarantee that you
can write these stuff with the language itself. :) When you use strcat()
don't necessarily imagine a for loop looking for the first occurence of
'\0'. This would be very slow for very large strings. A method i am now
thinking is memcpy() some large blocks and search for '\0' in each of
these blocks, but as i said a compiler makes the memory management
itself and compiler stuff may include things more close to the machine
than these we write outside of the compiler.
> can determine the end is to find the null terminator, which=20
> will be the first 0 it finds. It won't look beyond that zero,=20
> so won't see any other text or zero's that may exist. Once it=20
> has found the end of the first string it will add on the=20
> characters from the second, overwring the null terminator in=20
> the first and any other stuff that is there. It will go on=20
> till it finds the NULL on the second string and will be sure=20
> to add a NULL onto the concatenated string. Here is a short=20
> function that probably does the same as the standard one.
>=20
> MyStrCat(char *str1, const char *str2)
> {
> while(*str1) ++str1; // find end of first string
> // out now points to the null of out
> while(*str2)
> *str1++ =3D *str2++; // copy second string
> // to end of first.
> *str1 =3D 0; // null terminate
> }
That's nice for a first approach but imagine that you had a string with
5 thousand characters... As i said you can write even faster function
using C or C++ but it can be written in assembly too in something like
that:
inline void MyStrCat(char *str1, const char *str2)
{
asm("Bizarre code here");
....
}
It can be even "worse". :) Stroustrup writes in Special Edition that
they may be no header files at all and #include statements can work as
on/off switches!
E.g..
#include <cstdio> // switch that makes the cstdio library functions
accessible.
That means they are all automatically accessible and you define
accessibility using #include statements. :)
>=20
> Note that if you pass two pointers to the same string you=20
> will get an infinate loop.
>=20
> Regards Jason.
>=20
> PS: guess I've gone OTT again.... oops!
What's OTT? :)
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|