|
From: Ioannis V. <no...@ya...> - 2001-10-25 11:07:49
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Jason Hardman
> Sent: Wednesday, October 24, 2001 11:59 PM
> To: Dev-C++
> Subject: Re: [Dev-C++] Errorhandling
>=20
>=20
> While I don't want to kick up a fuss in this discussion I=20
> would like to point out firstly that search techniques in=20
> general rely on the structure to be searched being sorted=20
> into some specific order. It would be unreasonable to sort a=20
> string just so you can find the instance of a null, and it=20
> would invalidate the result anyway. ;) There isn't much more=20
> you can do in searching a string except by going in sequence=20
> through it. I know it has been mentioned that maybe a=20
You are talking about ANSI C way. But compiler stuff can be written in a
system dependend and more close to the machine way, so it can use win32
for example. It can use assembly, and generally the compiler knows
*more* than us from outside the compiler. As a hint take for example
sizeof. sizeof even for ANSI standard knows the length of an array in
the scope it was defined. How does it know? It's compiler stuff and
compiler knows. So strcat() to search character after character would be
stupid for a nowadays compiler. But it is *safe* to assume it looks
character after character to find the '\0'. As it is safe to assume it
is written in C doing that, but of course it can be written in whatever
language the compiler author wants (assembly, C++, ...). Usually C and
C++ are the case but it could be even fortran for some special cases
(precompiled the implementation file).
> compiler has optimisations to work around searching the=20
> entire length of a string to find it's end, but it's not=20
> something you can rely on. Adding these features would=20
> introduce a great deal of complexity into the compiler, but=20
> that is up to whoever writes it. I think you will find that=20
> the majority of compilers will actualy deal with string=20
> operations in the way you would expect from an external=20
> viewpoint. You will note that that standard C/C++ does not=20
Yes that's what i said.
> rely on knowing the lengths of arrays and will overrun their=20
ANSI C/C++ know the length of the arrays in the scope they were declared
using sizeof.
> ends if you are not careful. I would think that if the
> compiler had extra info about a string like it's length then=20
> it would use it in cases like this. In the end it's not=20
Yes it probably does.
> something I have to worry about.
I agree very much. :)
> Secondly, the use of the '\0' character is fairly limited,=20
> but there are some cases where it may be used. I agree that=20
> using it to manualy null terminate a string literal is bad=20
> practice and should be avoided.
>=20
> Possibilities for use of '\0' are:
>=20
> Manualy null terminating a string, i.e.
>=20
> char str[27];
> for(char i =3D 0; i < 26; ++i) str[i] =3D 'A' + i;
> str[26] =3D '\0';
> cout << str;
>=20
> Of course you could just as easily use a zero, but '\0' is in=20
> context to the operation this way.
Yes i agree but this is another case.
> or
>=20
> unsigned char str[] =3D {'\200', '\250', '\210', '\300', '\0'};
>=20
> I've not looked up what those numbers do, but generally there=20
> are additional characters after 127 decimal (177 octal) that=20
> are special, i.e. graphics characters. I remind you that a=20
> backslash followed by a number is an octal number and not a decimal.
A number having a \ is an escape sequence, *not* an octal number. 0200
is an octal number (without ' ').
=20
> Also I could envisage using it within a string literal as a=20
> way of breaking it up i.e. "Hello.\0This is a test.\0Break!",=20
Yes, but this is another case.
> of course I wouldn't recomend using a structure like this=20
> generally, but it's not impossible to envision a need for it.=20
> You would have to parse it manualy to obtain the strings or=20
> other data from it.
>=20
> Just some thoughts, Jason.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|