Author Topic: String array size  (Read 2726 times)

0 Members and 1 Guest are viewing this topic.

Offline bugmagnet

  • Newbie
  • Posts: 6
    • View Profile
String array size
« 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

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: String array size
« Reply #1 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.
« Last Edit: August 09, 2019, 11:00:57 am by Petr »

Offline bugmagnet

  • Newbie
  • Posts: 6
    • View Profile
Re: String array size
« Reply #2 on: August 10, 2019, 09:59:16 am »
Works as advertised. Thanks very much.