|
From: Jason H. <jas...@bt...> - 2001-10-20 12:55:19
|
OK then that's agreed. And I admit I did once use sizeof to determine the length of a string array, but only cos it was a string of constant length, although I can't remember if I created it as a constant string. I shall never transgress again. ;-D I wonder if the original poster has been paying any attention to us two. I hope at least he has learnt that you don't need to null terminate a string literal by hand. Jason. ----- Original Message ----- From: "Ioannis Vranos" <no...@ya...> To: "Dev-C++" <dev...@li...> Sent: Saturday, October 20, 2001 12:30 PM Subject: RE: [Dev-C++] Errorhandling At first, we do not argue we discuss. It is fun to discuss for C and C++. :) Anyway, the standard doesn't say that it looks character after character (compiler has its own internal memory management and it may mark large blocks of memory with length numbers and stuff: read the 8th chapter of K&R 2 and in particular 8.7 where it has an implementation of malloc) but you are probably right, it probably behaves as if it looks from character to character. But the main point here is that strcat(buffer, "a string\0"); is worthless and is looking for trouble. I am 100% sure that in certain occasions it means trouble. E.g. like you use sizeof on a char array to determine the length of the string and not strlen() as i saw in some email in this list. In summary using "string\0" shows lack of knowledge at least. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys > -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Jason Hardman > Sent: Saturday, October 20, 2001 1:38 AM > To: Dev-C++ > Subject: Re: [Dev-C++] Errorhandling > > > As defined in C/C++ Programmers Reference: > > strcat > ------ > #include <string.h> > char *strcat(char *str1, const char *str2); > > The strcat() function concatenates a copy of str2 to str1 and > terminates str1 with a null. The null terminator originally > ending str1 is overwritten by the first character of str2. > The string str2 is untouched by the operation. If the arrays > overlap, the behavior of strcat() is undefined. > > In C99, str1 and str2 are qualified by restrict. > > The strcat() function returns str1. > > Remember, no bounds checking takes place, so it is the > programmer's responsibility to ensure that str1 is large > enough to hold both its original contents and also those of str2. > ------------------------ > > The function strcat() is defined in the C, and therefore the > C++, standard. It must follow a consistent behaviour. It is > feasible that it could have been written in assembler, but it > will still have the same basic behaviour. I know it would be > slow in large strings, but there isn't really any faster and > reliable way to search for the null. Skipping characters > opens up the possibility of missing the proper null, and > accessing characters after it which will be undefined. In > strcat you might not overrun the array containing the string > that way, but in a similar function such as strlen you > probably would, and that would be an access violation. > > Without more information about the string being passed then > you are pretty much limited to going throught the string from > start to finish. Generally if a programmer was dealing with > very large string then it would be wise for him/her to find a > better way of dealing with them (such as tracking the > length), or to just accept the performance hit. > > Whether the include is an on/off switch or a header won't > change the way the function works, just how the compiler adds > it to the exe. > > Regards, Jason. > > PS: Yet again I've probably gone OTT (Over The Top). LOL! It > looks like I've written an essay on a standard function. > > PPS: Why is it us to always seem to be arguing over silly > little things like this? > > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > _______________________________________________ Dev-cpp-users mailing list Dev...@li... https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |