tags:

views:

278

answers:

4

why the variables are not initializing to zero in fortran 2003 when comapared with f90?

i have a variable in a funtion from a file. its initialized to 0. I want to use it another funtion then it shows a garbage value. even for global variables also. Is there any option I need to set for fortran 2003 compiler?

+1  A: 

I'm unfamiliar with any Fortran compiler but I do know that in general, most compilers won't initialize global and local variables. Initialization should always be done in code. You should not rely on the compiler to do this for you. The garbage you're seeing is probably from the stack or memory heap. Some compilers will fill the heap with zero's when allocating memory which could explain why some compilers will seem to initialize variables with 0. They haven't actually initialized anything, they're just using a memory area that happened to be filled with zero's...

Workshop Alex
Wow--unfamiliar with any Fortran compiler but still willing to offer an opinion. Just wow.
Onorio Catenacci
I know how compilers work, yes. Have lots of experience with compilers in general, so Fortran isn't much different. Since the question doesn't mention which Fortran compiler is used, it's just a bit generic after all.
Workshop Alex
+3  A: 

You could try using -zero or /Qzero -- these will initialize local scalars to zero -- but you really should be explicitly setting initial values. Depending on the compiler to do it for you is, as you have found out, a good way to introduce bugs. Note that the option names may be different for different compilers. The ones mentioned are for Intel Visual Fortran.

tvanfosson
+4  A: 

There is no difference between Fortran 90 and Fortran 2003 in initialisation of variables. All valid Fortran 90 code is valid Fortran 2003, and should give the same result (except for very few obscure corner-cases where what was compiler-dependant behaviour is now specified by the standard; this is not one of those).

Now, as to why you could see a difference, it's hard to say without knowing what your compilers are, and what your code does exactly. I strongly suspect you were relying on compiler-dependant behaviour, and it broke when you changed compiler.

FX
+3  A: 

We experienced this moving from Compaq Visual Fortran to Intel Visual Fortran. Notwithstanding his lack of familiarity with Fortran compilers, the entire post left by Workshop Alex is correct--you shouldn't rely on the compiler setting initial values. The Standard doesn't say variable values should automatically be set. Even if it did, relying on this compiler behavior is risky.

Compaq Visual Fortran automatically initializes variables. Other compilers do not. Your code needs to be fixed. You can only do that by initializing all your variables.

John

John Reynolds