|
From: Jason H. <jas...@bt...> - 2001-10-25 22:05:40
|
MessageYou cannot always know the length of an array or string at = compile time. It is possible for things like array size and such to be = determined at compile-time and at run-time (depending), but by doing it = at run-time you are incurring an overhead in your code, which isn't part = of the C style of programming. Adding these mechanisms to C code with = hidden compiler features would be breaking the general C/C++ ethic, at = least in my view. The only reason I'm 'discussing' this is because the more I think about = it the more I come to believe that it's more likely that standard = functions like strcat() and strlen() will determine the length of the = string by going all the way through the string until null. I know this = is an old argument, but I find it difficult to believe that it would be = done a different way. C doesn't actualy have native support for strings, = it just uses an array of chars, and allows the definition of string = literals. In Pascal, for example, it does have native support for strings and you = will find that it will store a pointer to an array and the first element = of the array will be the length of the string that follows. If you want we can end this particular argument and settle on the fact = that we are both willing to differ in opinion, and that it doesn't = matter which is true 'cos it still works no matter what. Jason. ----- Original Message -----=20 From: Ioannis Vranos=20 To: Dev-C++=20 Sent: Thursday, October 25, 2001 10:54 PM Subject: RE: [Dev-C++] Errorhandling Isn't it possible for an internal "sizeof style" mechanism that = determines the size of the data and insert it as a constant value in any = function, at a specific system? Btw for what about are we arguing for? (If we are arguing) :) 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: Thursday, October 25, 2001 4:13 PM To: Dev-C++ Subject: Re: [Dev-C++] Errorhandling > 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'. It's important to realise that sizeof is a special compile time = operator. When the compiler finds it it determines the size of the data = refered to and inserts it as a constant value. The sizeof operator won't = work at runtime. For it to work at runtime the compiler would have to = include a lot of extra code and information about a data type created to = help detemine sizes, and that wouldn't follow the standard AFAICT. To do = that would turn C/C++ into a higher level "safe" language that is also a = lot slower. So what does strcat() or strlen() do when you pass it a = dynamically allocated array that the compiler didn't know about at = compile-time? It won't use the sizeof. =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 ' '). I was refering to the fact that the number following the backslash = is interpreted as an octal constant and not as a decimal; it doesn't = need the preceding zero. Also if you write '\xff', that is a hexadecimal = character constant. Regards, Jason. |