window closing/odd behavior

General FreeBASIC programming questions.
Post Reply
JLThompson999
Posts: 12
Joined: Jan 07, 2024 4:22

window closing/odd behavior

Post by JLThompson999 »

I am trying to rewrite a lengthy program I wrote in Liberty Basic for Freebasic. I have encountered a strange problem. When I try to run this code:

Code: Select all

Dim np$(30) as string, pncttn$(30) as string
Dim wd$(10000) As String, iwd$(30000) As String, dwd$(5000) As String, idwd$(30000) As String

Dim insen$ as STRING, cw AS INTEGER, f as integer

declare SUB innomeOne(byref insen$ as string , byref cw as integer , np$() as string , pncttn$() as string)

Input "Tell me something: ", insen$
innomeOne(insen$ , cw , np$() , pncttn$())
print np$(1)
print np$(2)
print np$(3)
print np$(4)
print np$(5)
print cw

f=1

sleep

SUB innomeOne(byref insen$ as string , byref cw as integer , np$() as string , pncttn$() as string)

'' this nome processes the input sentence for use by the rest of the program
'' it removes punctuation and puts everything in lower case
'' it counts the number of words in the input sentence, remembering it in cw
'' it also stores the individual words in np$() and the punctuation of each word in pncttn$()

dim islen as INTEGER, thisword$ as string, thswdln as integer, x as integer

insen$=LCase(insen$)
islen=len(insen$)

cw=1
thisword$=""
For x=1 To islen
  If Mid(insen$, x, 1) <> " " then
    thisword$=thisword$+Mid(insen$, x, 1)
  EndIf
  If Mid(insen$, x, 1) = " " then
    np$(cw)=thisword$
    If Right(np$(cw), 1) = "." Or Right(np$(cw), 1) = "?" Or Right(np$(cw), 1) = "!" Or Right(np$(cw), 1) = "," Or Right(np$(cw), 1) = ";" Or Right(np$(cw), 1) = ":" then
      thswdln=Len(np$(cw))
      pncttn$(cw)=Right(np$(cw), 1)
      np$(cw)=Left(np$(cw), thswdln-1)
    EndIf
    thisword$=""
    cw=cw+1
  EndIf
Next
np$(cw)=thisword$
If Right(np$(cw), 1) = "." Or Right(np$(cw), 1) = "?" Or Right(np$(cw), 1) = "!" Or Right(np$(cw), 1) = "," Or Right(np$(cw), 1) = ";" Or Right(np$(cw), 1) = ":" then
  thswdln=Len(np$(cw))
  pncttn$(cw)=Right(np$(cw), 1)
  np$(cw)=Left(np$(cw), thswdln-1)
EndIf
thisword$=""

End Sub
it runs as it should. But when I add one more array to the second line, making it:

Code: Select all

Dim wd$(10000) As String, iwd$(30000) As String, dwd$(5000) As String, idwd$(30000) As String, awd$(30000) As String
it won't run properly anymore. Instead, the window closes immediately after opening, without printing out anything or allowing for input. Even odder, I get no error message, just the "Make done" message. Does anyone have any idea of what is going on?
Last edited by fxm on Feb 12, 2024 6:09, edited 1 time in total.
Reason: Added code tags.
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

Re: window closing/odd behavior

Post by shadow008 »

Not at my computer so I can't test but my WAG is a stack overflow. 75,000 x 24 (sizeof string on 64 bit) =1,800,000 bytes which is rather close to the 2MB default stack size compiling for 64 bit https://freebasic.net/wiki/CompilerOptt
If you're on windows you should check the event viewer for the error it crashed on.

Try allocating those strings on the heap with new[] (and delete them when done of course) and see if that fixes it. Alternatively, increase the stack size.
fxm
Moderator
Posts: 12134
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: window closing/odd behavior

Post by fxm »

You can more simply define your arrays as dynamic (array data in the heap) by simply using 'Redim' instead of 'Dim'.
SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: window closing/odd behavior

Post by SARG »

Compiling with -gen gas64 you would get this warning :

Code: Select all

\test2.bas() warning 24(3): Variable too large for stack, consider making it SHARED proc=main STACK OVERFLOW, review array size, use redim/shared or increase stack size
The stack overflow is detected at compilation time. This feature is only available with gas64.
Post Reply