QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: bugmagnet on August 09, 2019, 09:55:23 am

Title: String array size
Post by: bugmagnet on August 09, 2019, 09:55:23 am
Is there a limit to how large a string array can be? Or is my problem just due to the way in which I am loading the array?

I'm using QB64 on Linux, specifically Linux version 4.15.0-55-generic (buildd@lcy01-amd64-029) (gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)) #60-Ubuntu

Code: QB64: [Select]
  1. '$DYNAMIC
  2.  
  3. DIM words(1 TO 1000) AS STRING
  4. DIM wordCount AS INTEGER
  5. DIM newBound AS INTEGER
  6. DIM inputFile AS STRING
  7.  
  8. inputFile = COMMAND$(1)
  9. IF inputFile = "" THEN inputFile = "wordlist.txt"
  10.  
  11. wordCount = 1
  12. OPEN inputFile FOR INPUT AS #1
  13.     LINE INPUT #1, word
  14.     words(wordCount) = UCASE$(word)
  15.     wordCount = wordCount + 1
  16.     IF wordCount > FIX(UBOUND(words) - (UBOUND(words) / 10)) THEN
  17.         newBound = UBOUND(words) * 2
  18.         REDIM _PRESERVE words(1 TO newBound)
  19.     END IF
  20.  
  21. PRINT wordCount - 1; "words loaded"

I downloaded the large words.txt from https://github.com/dwyl/english-words and attempted to load that into memory as per the above. It crashed with a core dump.

-- bugmagnet
Title: Re: String array size
Post by: Petr on August 09, 2019, 10:44:47 am
Try this:

Code: QB64: [Select]
  1. '$DYNAMIC
  2.  
  3. DIM words(1 TO 1000) AS STRING
  4. DIM wordCount AS LONG
  5. DIM newBound AS INTEGER
  6. DIM inputFile AS STRING
  7.  
  8. inputFile = COMMAND$(1)
  9. IF inputFile = "" THEN inputFile = "wordlist.txt"
  10.  
  11. wordCount = 1
  12. OPEN inputFile FOR INPUT AS #1
  13.     LINE INPUT #1, word
  14.     words(wordCount) = UCASE$(word)
  15.     wordCount = wordCount + 1
  16.     REDIM _PRESERVE words(wordCount)
  17. REDIM _PRESERVE words(wordCount - 1)
  18.  
  19. PRINT wordCount - 1; "words loaded. Total records:"; UBOUND(words)
  20.  
  21.  

Limit for arrays? Size of your RAM.

For file words.txt, which is 4MB big, use counter WordCount as LONG type. FIX is just for INTEGER value and this is too small type for so long file.
Title: Re: String array size
Post by: bugmagnet on August 10, 2019, 09:59:16 am
Works as advertised. Thanks very much.