Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to initialize array in C/C++?

I recently encountered a case where I need to compare two files (golden and expected) for verification of test results and even though the data written to both the files were same, the files does not match.

On further investigation, I found that there is a structure which contains some integers and a char array of 64 bytes, and not all the bytes of char array were getting used in most of the cases and unused fields from the array contain random data and that was causing the mismatch.

This brought me ask the question whether it is good practice to initialize the array in C/C++ as well, as it is done in Java?

like image 222
sand Avatar asked Jan 11 '10 06:01

sand


4 Answers

It is good practice to initialise memory/variables before you use them - uninitialised variables are a big source of bugs that are often very hard to track down.

Initialising all the data is a very good idea when writing it to a file format: It keeps the file contents cleaner so they are easier to work with, less prone to problems if someone incorrectly tries to "use" the uninitialised data (remember it may not just be your own code that reads the data in future), and makes the files much more compressible.

The only good reason not to initialise variables before you use them is in performance-critical situations, where the initialisation is technically "unnecessary" and incurs a significant overhead. But in most cases initialising variables won't cause significant harm (especially if they are only declared immediately before they are used), but will save you a lot of development time by eliminating a common source of bugs.

like image 89
Jason Williams Avatar answered Nov 16 '22 06:11

Jason Williams


Using an undefined value in an array results in undefined behaviour. Thus the program is free to produce differing results. This may mean your files end up slightly different, or that the program crashes, or the program formats your hard drive, or the program causes demons to fly out the users nose ( http://catb.org/jargon/html/N/nasal-demons.html )

This doesn't mean you need to define your array values when you create the array, but you must ensure you initialise any array value before you use it. Of course the simplest way to ensure this is to do this when you create the array.

MyStruct array[10];
printf( "%f", array[2].v ); // POTENTIAL BANG!
array[3].v = 7.0;
...
printf( "%f", array[3].v ); // THIS IS OK.

Don't forget that for huge arrays of PODs there's a nice shorthand to initialise all members to zero

MyPODStruct bigArray[1000] = { 0 };
like image 25
Michael Anderson Avatar answered Nov 16 '22 06:11

Michael Anderson


I strongly disagree with the given opinions that doing so is "eliminating a common source of bugs" or "not doing so will mess with your program's correctness". If the program works with unitialized values then it has a bug and is incorrect. Initializing the values does not eliminate this bug, because they often still do not have the expected values at the first use. However, when they contain random garbage, the program is more likely to crash in a random way at every try. Always having the same values may give a more deterministic behaviour in crashing and makes debugging easier.

For your specific question, it is also good security practice to overwrite unused parts before they are written to a file, because they may contain something from a previous use that you do not want to be written, like passwords.

like image 4
Secure Avatar answered Nov 16 '22 04:11

Secure


If you don't initialize the values in a c++ array, then the values could be anything, so it would be good practice to zero them out if you want predictable results.

But if you use the char array like a null terminated string, then you should be able to write it to a file with the proper function.

Although in c++ it might be better to use a more OOP solution. I.E. vectors, strings, etc.

like image 1
zmbush Avatar answered Nov 16 '22 04:11

zmbush