Global variable array - strange problem with freeBASIC 1.04.0 and 1.05.0 (Windows 32 Bit)

New to FreeBASIC? Post your questions here.
Post Reply
oog
Posts: 124
Joined: Jul 08, 2011 20:34

Global variable array - strange problem with freeBASIC 1.04.0 and 1.05.0 (Windows 32 Bit)

Post by oog »

If I declare a global array of integers, the array is usually initialized with 0-values and I can store any values in it. When I was working on file analysis, I found a strange bug. I reduced the code to a small example, which still shows the bug.

Sample program:

Code: Select all

Dim As Integer problemAddress = -1
Dim As String dummy_2(0 To 65535)
Dim As Integer testArray(0 To 65535)

Print "Test"

For i As Integer=0 To 65535
  If testArray(i)<>0 Then
    problemAddress = i
    Print "Unexpected value in array, problemAddress=";problemAddress;", value=";testArray(problemAddress)
  EndIf
Next i
If problemAddress <> -1 Then
  Print "Print testArray(problemAddress): ";testArray(problemAddress)
  Print "Let testArray(problemAddress) = 0"
  'try to write zero to the array
  testArray(problemAddress)=0
  Print "Print testArray(problemAddress): ";testArray(problemAddress)
Else
  Print "Everything is fine."
EndIf
Sleep
Sometimes this program crashes without any warning (compiling with -exx does not help).
If it does not crash, then there is an address within the array, that is not initialized to zero. I call this the problemAddress.
This element of the array can not be set to zero, as if it was write protected.

Example output of the program:

Code: Select all

  Test
  Unexpected value in array, problemAddress= 81, value= 22450
  Print testArray(problemAddress): 22450
  Let testArray(problemAddress) = 0
  Print testArray(problemAddress): 22450
I was working with freeBASIC 1.04.0, when I found this bug. So I did update to the actual 1.05.0, but the problem still remains. Is it a bug in the compiler or am I doing something wrong?

I noticed, that I can work araound the bug, if I declare the array as "shared". But if I do not fully understand what the problem is, I'm not sure if this is a solution or if this just moves the problem to another place / address.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Global variable array - strange problem with freeBASIC 1.04.0 and 1.05.0 (Windows 32 Bit)

Post by jj2007 »

It behaves weird, indeed. I am using 1.05.0 (01-31-2016), and your code runs fine with -gas, but when using gcc it crashes on entry, before even touching the print "int ok" command below. This snippet should not pose any problems, but it crashes immediately:

Code: Select all

'asm INT 3  ' activate breakpoint for debugging
Dim As Integer problemAddress = -1
asm nop	' serves to find the instructions
asm nop
Print "int ok"
asm nop
asm nop
Dim As String dummy_2(0 To 65535)
asm nop
asm nop
print "string ok"
Dim As Integer testArray(0 To 65535)
Sleep
From what I see, the stack is very low on entry. And indeed, if you comment out one of the two later Dims, dummy_2 or testArray, it runs fine. So the compiler does some stack fumbling based on what is needed later.

It works, though, with -gen gcc -t 1200 -Wc -O2 -s console (but crashes with -t 1000), meaning that probably the default stack allocation is not enough.
Last edited by jj2007 on Apr 20, 2018 7:17, edited 1 time in total.
adele
Posts: 47
Joined: Jun 13, 2015 19:33

Re: Global variable array - strange problem with freeBASIC 1.04.0 and 1.05.0 (Windows 32 Bit)

Post by adele »

Hi,

I got a Warning: (FB105/x64 Win)

"C:\PRG\FBC\fbc -i \prg\fbc\tdlib -p \prg\fbc\tdlib -s console -b "problemAddress.bas"
problemAddress.bas(3) warning 23(2): Array too large for stack, consider making it var-len or SHARED"

commenting out the String array:

'REM' Dim As String dummy_2(0 To 65535)

solved both problems: warning and crash
1 String needs (x64) Varptr+Structure=32 Bytes, Plus the Integer array might be too much at the same time ?

Adi
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Global variable array - strange problem with freeBASIC 1.04.0 and 1.05.0 (Windows 32 Bit)

Post by jj2007 »

adele wrote:1 String needs (x64) Varptr+Structure=32 Bytes, Plus the Integer array might be too much at the same time ?
Yes indeed, and -t 1200 solves that problem, as written above. I get that warning in FB64 but not in FB32. IMHO the compiler should recognise the problem, and even throw an error.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Global variable array - strange problem with freeBASIC 1.04.0 and 1.05.0 (Windows 32 Bit)

Post by fxm »

The FreeBASIC compiler does not add code to check at runtime the available memory in the system stack (even with the -exx compile option).
Only at compile time it roughly estimates if for each local static array there is no overflow of the system stack but without taking into account the stack memory needed for other local variables in the scope (including the other local arrays in the scope).
Compiler warning occurs only for each local static array strictly greater than the complete system stack size.

Otherwise, see FAQ: Why does my program crash when I define an array larger than xx?
paul doe
Moderator
Posts: 1735
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Global variable array - strange problem with freeBASIC 1.04.0 and 1.05.0 (Windows 32 Bit)

Post by paul doe »

oog wrote:If I declare a global array of integers, the array is usually initialized with 0-values and I can store any values in it. When I was working on file analysis, I found a strange bug. I reduced the code to a small example, which still shows the bug.
It's not a bug. Like the Wiki states, you need to declare these arrays on the heap:

Code: Select all

Dim As Integer problemAddress = -1
dim as string dummy_2( any )
redim dummy_2( 0 to 65535 )
'Dim As String dummy_2(0 To 65535)
dim as integer testArray( any )
redim testArray( 0 to 65535 )
'Dim As Integer testArray(0 To 65535)

Print "Test"

For i As Integer=0 To 65535
  If testArray(i)<>0 Then
    problemAddress = i
    Print "Unexpected value in array, problemAddress=";problemAddress;", value=";testArray(problemAddress)
  EndIf
Next i
If problemAddress <> -1 Then
  Print "Print testArray(problemAddress): ";testArray(problemAddress)
  Print "Let testArray(problemAddress) = 0"
  'try to write zero to the array
  testArray(problemAddress)=0
  Print "Print testArray(problemAddress): ";testArray(problemAddress)
Else
  Print "Everything is fine."
EndIf
Sleep
Like this. No more problems ;)
oog
Posts: 124
Joined: Jul 08, 2011 20:34

Re: Global variable array - strange problem with freeBASIC 1.04.0 and 1.05.0 (Windows 32 Bit)

Post by oog »

Thanks for the explanations. Especially the link to the FAQ was very helpful.
Post Reply