v056ped5@ubvmsd.cc.buffalo.edu (Brian M McNamara) (03/28/91)
I was writing a program today that maintained an array of structures. Near the end of writing the program, I realized that i needed one more int variable in the structure. Everytime I compiled the program, it worked correctly once. The second time I ran the program, garbage showed up in those fields rather than the initial zeros i wanted. After I realized what the problem was, debugging was obviously simple. Why do programs work once when you have a bug like this? I have see this same problem in many different languages using many different compilers. This one was through Turbo C's editor environment. Also, why don't they write compilers so that numeric data is initialized to zero and character data is initialized to a single EOL? Brian
scs@adam.mit.edu (Steve Summit) (03/28/91)
In article <67693@eerie.acsu.Buffalo.EDU> v056ped5@ubvmsd.cc.buffalo.edu writes: >Also, why don't they write compilers so that numeric data is initialized >to zero and character data is initialized to a single EOL? Perhaps the simplest answer, though not terribly satisfying, is because "it's not done that way." Actually, statically-allocated data is guaranteed to be initialized to 0. (This translates to NULL pointers for character pointers, and '\0' for character arrays.) If you want consistent, predictable (though not necessarily useful) behavior for dynamically-allocated memory, use calloc() (but see the FAQ list for caveats, and more information about initialization and allocation in general). Given the realities of dynamic memory allocation, a better question might be, "why don't they write compilers so that numeric data is initialized to 2574 and character data is initialized to '\007'?" Steve Summit scs@adam.mit.edu