How does one find the maximum length of a scalar default character variable allowed by a compiler? I was disappointed that iso_fortran_env
seems not to supply it.
I’m not sure there is a standard way to query this value. This is described typically in the compiler documentation. For example, the nagfor documentation says:
Maximum character length (except as below)
= 2147483647
Maximum character length (64-bit Windows and -abi=64c Linux)
= 1099511627775
For Intel Fortran these are given here: Compiler Limits
Character lengths
2**31-1 on systems using IA-32 architecture
2**63-1 on systems using Intel® 64 architecture
In practice it might not be the case (I’m on an x64 architecture):
program test_len
implicit none
integer(8) :: n, i
integer :: stat
character(len=:), allocatable :: str
n = 1
i = 0
do
allocate(character(len=n) :: str, stat=stat)
print '("n = 2**",I2," = ",I20,1X,", stat = ", L1)', i, n, stat == 0
if (stat /= 0) exit
n = 2 * n
i = i + 1
deallocate(str)
end do
end program
$ ifort test_len.f90
$ ./a.out
n = 2** 0 = 1 , stat = T
n = 2** 1 = 2 , stat = T
n = 2** 2 = 4 , stat = T
n = 2** 3 = 8 , stat = T
n = 2** 4 = 16 , stat = T
n = 2** 5 = 32 , stat = T
n = 2** 6 = 64 , stat = T
n = 2** 7 = 128 , stat = T
n = 2** 8 = 256 , stat = T
n = 2** 9 = 512 , stat = T
n = 2**10 = 1024 , stat = T
n = 2**11 = 2048 , stat = T
n = 2**12 = 4096 , stat = T
n = 2**13 = 8192 , stat = T
n = 2**14 = 16384 , stat = T
n = 2**15 = 32768 , stat = T
n = 2**16 = 65536 , stat = T
n = 2**17 = 131072 , stat = T
n = 2**18 = 262144 , stat = T
n = 2**19 = 524288 , stat = T
n = 2**20 = 1048576 , stat = T
n = 2**21 = 2097152 , stat = T
n = 2**22 = 4194304 , stat = T
n = 2**23 = 8388608 , stat = T
n = 2**24 = 16777216 , stat = T
n = 2**25 = 33554432 , stat = T
n = 2**26 = 67108864 , stat = T
n = 2**27 = 134217728 , stat = T
n = 2**28 = 268435456 , stat = T
n = 2**29 = 536870912 , stat = T
n = 2**30 = 1073741824 , stat = T
n = 2**31 = 2147483648 , stat = T
n = 2**32 = 4294967296 , stat = T
n = 2**33 = 8589934592 , stat = T
n = 2**34 = 17179869184 , stat = T
n = 2**35 = 34359738368 , stat = T
n = 2**36 = 68719476736 , stat = T
n = 2**37 = 137438953472 , stat = T
n = 2**38 = 274877906944 , stat = T
n = 2**39 = 549755813888 , stat = T
n = 2**40 = 1099511627776 , stat = T
n = 2**41 = 2199023255552 , stat = T
n = 2**42 = 4398046511104 , stat = T
n = 2**43 = 8796093022208 , stat = T
n = 2**44 = 17592186044416 , stat = T
n = 2**45 = 35184372088832 , stat = T
n = 2**46 = 70368744177664 , stat = T
n = 2**47 = 140737488355328 , stat = F
To learn why it fails at 2^{48-1}, see this section: 64-bit computing - Wikipedia
The x86-64 architecture (as of 2016) allows 48 bits for virtual memory and, for any given processor, up to 52 bits for physical memory. These limits allow memory sizes of 256 TiB (256 × 1024^4 bytes) and 4 PiB (4 × 1024^5 bytes), respectively.
In reality, even the true lines in the output are false, because my computer only has 16 GB of RAM, and 300 GB of free storage, which could be used as swap space. This is roughly 2^{38.2} bytes or character elements.