Fortran

Guide To Learn

When we’re done working with the array, we can clean it from memory like this:

deallocate(a)

After issuing deallocate, you must allocate array a again before using it on the right side of assignments. We’ll apply this mechanism to reuse arrays between different stocks.

Automatic deallocation

An allocatable array is automatically deallocated when it goes out of scope. For example, if you declare and allocate an array inside of a function or subroutine, it will be deallocated on return.

Much like it’s an error to allocate an object that’s already allocated, it’s also an error to deallocate an object that’s not allocated! In the next subsection, I explain how you can check the allocation status so you’ll never erroneously allocate an object twice or deallocate an object that hasn’t even been allocated yet.

Otherwise, there’s no restriction with regard to whether the array has been initialized or not. You’re free to deallocate an uninitialized array; for example, if you learn that the array is not of the expected size, or similar.

Tip Deallocate all allocatable variables when you’re done working with them.

The diagram in figure 5.2 illustrates a life cycle of a dynamic array.

We first declare the array as allocatable. At this point, the array isn’t yet allocated in memory, and its size is unspecified. When ready, we issue the allocate statement to reserve a chunk of memory to hold this array. This is also where we decide the size of the array or the start and end indices (in this example, 3 and 8). If not allocating from another source array, the values will be uninitialized. We thus need to initialize the array before doing anything else with it. Finally, once we’re done working with the array, we issue the deallocate statement to release the memory that holds the array. The status of the array is now back to unallocated, and it’s available for allocation. You can reuse a dynamic array like this any number of times, even with different sizes or start and end indices. This is exactly what we’ll do in our stock price analysis app. For each stock, we’ll allocate the arrays, use them to load the data from files, work on them, and then deallocate them before passing them on to the next stock.

Figure 5.2 A life cycle of a dynamic array

Careful with frequent allocation!

Arrays are contiguous in memory. This has pros and cons. On one side, indexing an array is a fast operation (constant in time, O(1)) because the computer can predict the position in memory of an element solely based on the index. On the other side, inserting, appending, or removing elements always triggers a reallocation of the whole array! The cost of this operation is proportional to the array size (linear in time, O(n)). For small arrays, this may be insignificant. However, appending an element to a 100 million-element-long array will trigger reallocation of ~400 MB! This can easily turn into a performance bottleneck in your app if you’re not careful.

Cleaning up after use

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top