I was on my master training on the second semester, I have to optimize a program, doing this I have a very strange bug : I forgot to inialize an array. And this was the big mistake, I had a lot of troubles to find out in which part of the code the mistake was located. In fact what was happening is that I initialized a big array before initalizing a smaller one, and got the data contained in this array. So when I changed the structure of this array, I had a lot of troubles that I couldn't explain.
How could this occure and what are the consequence
Knowing the content of an array, having this initialization that is all time done in the same way could be good starting values. The probleme arise when we did an accidental initialization of the datastructure. This initialization could even be quite constant if we don't change too much code, once the initialization is done the program will work proprelly until we change slighlty the previous used data structure. The worste was that I was expecting to initialize the structure before using it, but I didn't do so their was only one step with this unexpected data, and this brough big problem, long after I did modification in this part of code. The point is that if it was "new" memory from the system the result of the program would be quite random, but it was not the case, it was previously allocated memory.
How to avoid this problem
Their are too approach to avoid the occurence of this probleme :
- Avoiding to create the probleme : doing initialization by hand before we use the variables
- using debugger that are able to detecte usage of non initialized memory, once it's detected, it's generally a good idea to initalize it as early as possible
The first point could be avoided by using languages like java that perform the initialization before we could use the variables.
Conclusion
To conclude this discovery make me understand how much important it's to do proper initialization of variables. But their was another more funny part on this, discovering this I discovered also that the default initialization might be an inapropriate way to inialize the data.