RE: ANSI standards


Subject: RE: ANSI standards
From: Henrik Berg (henrik@lansen.se)
Date: Mon Jan 24 2000 - 05:04:41 CST


> Ah! I see what you mean. Is there any way around it though? If I check
> the length of the string, either by using strlen(), or a routine of my own
> making, won't it break on non-null-terminated strings too (the only way I
> can see of doing it is to loop through the string until I find a NULL)?

No, please don't. Think of this:

char *str1 = "Ahrrrrggggg... -> 1000 mor chars";
char *str2 = "Not like str1";
strcmp(str1, str2);

In this case strcmp compares one char with an other and returns. Adding a strlen, adds length of str1 itterations (plus length of str2).

> > if there's no problem, great... I am totally in favour regarding
> > simplification of code :) but one must not forget stability.

I'm totaly for using snprintf, strndup and other modern string writing functions, but string reading has not that problem.

> I totally agree with this. Perhaps I'm missing something obvious here - I
> think I need to go to bed :-)

I don't think you are missing anything. Checking the length is not needed it you don't write to the buffer. If out write oyou can overshoot the end of string and go into segfault country, but reading allways ends on the nul char. If someone uses strcmp he/she must input nul terminated stings (at least the shortest :)



This archive was generated by hypermail 2b25 : Mon Jan 24 2000 - 05:17:39 CST