Messages in this thread | | | Date | Tue, 29 Feb 2000 14:25:29 -0600 | From | Bill Wendling <> | Subject | Re: string-486.h? |
| |
Also sprach Richard B. Johnson: } On Tue, 29 Feb 2000, Bill Wendling wrote: } } > Depends on where the char str[12] is placed. If it is global, then it is } > zeroed cause it's static...If it's an auto, then it can (and probably } > does) have garbage after the terminating \0. } > } } No! I showed exactly what happened. I did not guess, nor was it theory. } I showed exactly what happens. The generated code is exact, explicit, } absolute. It is not what 'C' is required to care about. It is not what } some "rule" says. It is exactly and precisely what the 'C' compiler } did with the code cited by one who submitted the sample. } } I simply explained what was happening. Now, if you don't think it should } have done this then that's another matter. Since there is no known rule } about what the compiler should have done it could have left it alone } after the required terminating \0 or even written a copyright message in } the remaining string space. } } Most real programmers would not have used such a construct because it } requires that strings be duplicated. You need the string template } someplace known only to the compiler, then it has to be copied at } runtime to the place defined by the programmer. Normally one would } use an initialized pointer or just a static string declaration. } From the comp.lang.c FAQ:
Section 17. Miscellaneous
17.1: What can I safely assume about the initial values of variables which are not explicitly initialized? If global variables start out as "zero," is that good enough for null pointers and floating-point zeroes?
A: Variables with "static" duration (that is, those declared outside of functions, and those declared with the storage class static), are guaranteed initialized to zero, as if the programmer had typed "= 0". Therefore, such variables are initialized to the null pointer (of the correct type) if they are pointers, and to 0.0 if they are floating-point.
Variables with "automatic" duration (i.e. local variables without the static storage class) start out containing garbage, unless they are explicitly initialized. Nothing useful can be predicted about the garbage.
Dynamically-allocated memory obtained with malloc and realloc is also likely to contain garbage, and must be initialized by the calling program, as appropriate. Memory obtained with calloc contains all-bits-0, but this is not necessarily useful for pointer or floating-point values (see question 3.11, and section 1).
Sorry to quote you chapter and verse, but your dressing down required it, me thinks.
-- || Bill Wendling wendling@ganymede.isdn.uiuc.edu
- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/
| |