Silverfrost Forums

Welcome to our forums

CHARACTER variable size limit query

23 Mar 2010 12:19 (Edited: 24 Mar 2010 9:01) #6203

Does anyone know of any 'inherent' (i.e. not memory-related) limit on the length of a variable of type CHARACTER (len = ...)? Or any reason why code might behave unexpectedly if manipulating such variables with len = 2**16?

24 Mar 2010 6:10 #6206

It's not an array, it is a character variable. I don't know for sure but I think the limit for length is 1024, as I get different errors in a module compilation if length > 1024. You can certainly have an array of Character*1 much bigger. I have an array of 750 million and it works ok. John

24 Mar 2010 9:09 #6208

Quoted from JohnCampbell It's not an array, it is a character variable.

Whoops, of course it is; thanks. Subject line duly edited. I was going to call it 'String size limit query', then I remembered that string is not in the FORTRAN vocabulary (even though everyone would have known what I meant), then I realised I couldn't call it 'Character size limit query' (Answer: yes, 1). I think at that point I got flustered and strove for brevity at the expense of accuracy 😃

Quoted from JohnCampbell I don't know for sure but I think the limit for length is 1024, as I get different errors in a module compilation if length > 1024. You can certainly have an array of Character*1 much bigger. I have an array of 750 million and it works ok. John

Yes, I noted your latest post with interest. Did we just happen to cross in the post, as it were?

30 years since I first dipped my toe in the waters of FORTRAN, and I still look forward to the day when someone can explain to me, even by analogy, why it is necessary or desirable to distinguish between CHARACTER (len = n) char and CHARACTER char (n) ... why will one or the other not do?

24 Mar 2010 12:31 #6211

CHARACTER (len = n) char or CHARACTER*(n) char or CHARACTER char*(n) creates a character variable called 'char' that can hold 'n' characters an individual character can be addressed using char(i:i) groups of characters can be addressed using char(i:j)

CHARACTER char (n) or CHARACTER1 char(n) creates a character variable array containing 'n' elements that can each hold 1 character an individual character can be addressed using char(i) groups of characters can't so easily be addressed for assignments & comparisons etc and is akin to the pre-F77 method of storing each character in an integer1 array

It is probably better to use a different name for the variable than 'char' to avoid confusion with the intrinsic 'char' function. Ian

24 Mar 2010 11:34 #6213

I think I am wrong in my estimate of the limit of charactern, if my program is working as I expect. It shows a value of 200 million compiles and runs. The limit may be 2gb, meaning as much memory as available. Ian, I have demonstrated the difference between the variable 'character an' and array 'character b(n)*1'. Both have disadvantages as :

  1. setting a = ' ' can be inefficient, if you are only using a small part of a.

  2. Using the array in a subroutine call will not work if you want to receive as character ()

  3. using 'a' in an internal write or read may have other limitations, based on the maximum record size for an internal read/write buffer. Again I don't know what the limit is or where in Help to find the list of compiler limits.

       integer*4, parameter :: thousand = 1000
       integer*4, parameter :: million  = thousand*thousand
       integer*4, parameter :: char_len = 200*million
       character (len=char_len) aa
       character bb(char_len), c
       integer*4 i
    

    ! do i = 1,char_len c = char ( ichar('0')+mod(i,53) ) aa(i:i) = c bb(i) = c end do ! i = char_len-50 write (,'(a,i0,a,i0,a)') 'Variable is ',char_len,' (',char_len/million,'m)' write (,) 'Var: ',aa(i:) ! this is a single variable write (,) 'Array: ',bb(i:) ! this is 50 variables end

It's interesting that 'aa(i: )' and 'bb(i: )' both code and print the same, but are different. They can require a different format statement if not using *. You can convert from one to the other via an internal write, but only aa(i: ) can be easily used as the internal r/w buffer. I've always preferred the array format for large character data but the example above may show both are useable. John

ps : I increased the size to 1.5gb, removed the array bb and it still ran.

Please login to reply.