Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29274 Discussions

Is ALLOCATE statement suppose to initialize array?

oren_peles
Beginner
4,904 Views

Hi

 

I am trying to use dynamical allocation in fortran with ALLOCATE statement for array of integers. Is ALLOCATE suppose to initialize the array to zero?

 

Thanks

Oren

0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
4,904 Views

Initialization to 0 of allocated arrays (of integers) is an implementation issue. i.e. do not rely on it. The low level allocation typically calls on the C malloc. Depending on the runtime system you may get it wiped or not wiped. I would suggest you wipe the allocations for your self.

allocate(irray(1234))
irray = 0

Jim

0 Kudos
Steven_L_Intel1
Employee
4,904 Views
Actually, the answer to your question is no, ALLOCATE is not supposed to initialize the array. I have not yet run across an implementation which does such initialization. As Jim says, you are responsible for the initialization.
0 Kudos
jimdempseyatthecove
Honored Contributor III
4,904 Views

Additional note.

The allocation would not know what initialization value to use. Wiping to 0 is not always desired. Wiping to 0 may be of interest if the cells were to then be subsequently use to accumulate totals. 0's might not be a good choice if the purpose was to provide an indication that a cell was ever used since 0 may be a valid number for use. In this case a better choice would be to initialize to a "magic number" that would seldom, if not never, be used. e.g. initializing to hexidecimal 0xBAADBEEF. Or 0x80000000 (add 0's for size of int) which is arguably -0 because it has the property of (x .eq. -x).

Jim

0 Kudos
Reply