Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java.
Archive - Originally posted on "The Horse's Mouth" - 2011-05-05 04:54:23 - Graham Ellis
Starting with a clean slate. Are variables initisialised, and if so, how? Even with this fundamental question, languages vary considerably.
C and C++
From my (e)mailbag ...
"""In a piece of code we’ve written we declare an array, but we do not fill the elements with values, we assume (dangerous I know) that it has defaulted all elements to zero. During the code execution only a few elements of the array are set to an integer value BUT it seems on occasions that there are some elements that have values in other than zero. These elements are not referenced in the code to be changed, so we assume the problem lies with the declaration / initialisation of the array. On some days this happens continually from the start of the job running, but on other days when the code is loaded is does not happen at all."""
When you set up an array in C or C++, it won't be initialised unless you force it to be - it will take whatever values happen to be in left in the memory from precious actions. More often than not, that does turn out to be integer zeros as there's a lot of blank space in many (other) programs, and also many libraries / languages clear to zero on exit. For global arrays, it's going to be down to the specific linker / loader as to what it does. For anything allocated with calloc, the manual tells you it's initiiased, with malloc that is not the case - and (?) which does "new" use internally? For local variables, assume no intitialisation. Best / surest bet is, frankly, to loop in zeros as that will work no matter what's going on internally. If you have a lot of it to do, write the code just once as a function.
Some pointers there - hope they help. In summary, I'm not surprised that you've got a piece of code that works most of the time ... kinda typical for code that doesn't initialise when it should ;-/
Python and Ruby
Variables are NOT initisialised automatically, and if you try and use a variable before it exists, you'll get an error:
wizard:~ graham$ python
Python 2.7 (r27:82508, Jul 3 2010, 20:17:05)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> abc += 8
Traceback (most recent call last):
File "", line 1, in
NameError: name 'abc' is not defined
>>>
wizard:may11 graham$ irb
>> puts "Look at #{varname} please"
NameError: undefined local variable or method `varname' for main:Object
from (irb):1
>>
Perl and PHP
New variables are assumed to contain an empty string ... which (if you use them in a numeric context will be treated as zero.
If you run Perl with a -w command line switch of with use warnings;
you'll duely get warnings.
In PHP, you can set the E_NOTICE error level flagging to give you warnings if you use the contents of a variable that's not been initialised. See the PHP manual for how to set that - you can do so in your web server setup, your php.ini configuration file, or script by script.
Java
The Java compiler will throw out any possible attempt to use an uninitialised local variable:
wizard:may11 graham$ javac Flawed.java
Flawed.java:4: variable somevar might not have been initialized
System.out.print(somevar);
^
1 error
wizard:may11 graham$
and indeed that sometimes very irritating - if you initialise a variable in two different ways (in and if and else) some compilers will still complain and you'll need to explicitly set it to a value prior to the tonditional statment just to be overwritten one way or another within the conditions.
Arrays and member variables of objects are automatically initialised - to the default (fallback) value for their type - false for boolean, 0 for numerical types, null for reference types.