5.70 LTS x64: IMA using a global array in a procedure

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

5.70 LTS x64: IMA using a global array in a procedure

Post by Kurzer »

Hello everyone,

If an array is declared as global within a procedure and the array is accessed outside the procedure before the procedure is called, the compiler will not complain about this. The program is compiled "without" errors, but then crashes with a memory access error.

Code: Select all

EnableExplicit

Procedure Test()
   Global Dim MyArray.s(1)
   MyArray(0) = "Hello"
EndProcedure

; Test()

Debug MyArray(0)

Test()
For testing simply comment in/out the commented-out line 8.
The compiler should notice this

Kind Regards,
Kurzer
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by RSBasic »

I can confirm that.
Image
Image
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by mk-soft »

I don't think it's a bug.

Memory is only requested when DIM is called.
This is only done when calling in the procedure
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by Kurzer »

This is correct, but I would assign this kind of "problem" to the "EnableExplicit" area. So that the use of EnableExplicit warns me of such a constellation.
It's very similar to the constellation when I try to use an undeclared variable. This is what EnableExplicit warns me about.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by #NULL »

hehe :) I always thought you can't define a zero-size array. apparently it's possible (without ExtractJSON/XMLArray). 8)
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by Kurzer »

@Fred: We discussed this in the German forum and classified this type of coding as bad style.

In my opinion the compiler should prevent this kind of use of arrays. If a global array() is defined, this should only be allowed in the main scope.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by mk-soft »

Empty Array...

Code: Select all

EnableExplicit

Procedure InitArray(Size)
   Global Dim MyArray.s(Size)
EndProcedure

Debug ArraySize(MyArray())

InitArray(100)

Debug ArraySize(MyArray())
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by BarryG »

Code: Select all

Procedure Test()
   Global Dim MyArray.s(1)
   MyArray(0) = "Hello"
EndProcedure

; Test()

Debug MyArray(0)
Since Test() is never called, this means the array was never created; so instead of crashing with an illegal memory error, the compiler should give the usual warning of "MyArray() is not a function, array, list..." IMO.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by #NULL »

BarryG wrote:Since Test() is never called, this means the array was never created; so instead of crashing with an illegal memory error, the compiler should give the usual warning of "MyArray() is not a function, array, list..." IMO.
No, MyArray *is* an array, only with zero size. And the compiler cannot easily know if the function will ever be called.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by Sicro »

As @mk-soft has already pointed out, you must first check if the array has been declared before accessing the array, if the case exists that the procedure is not called:

Code: Select all

Procedure test()
  Global Dim value(2)
  value(0) = 1
  value(1) = 2
  value(2) = 3
EndProcedure

test()

If ArraySize(value()) > -1
  Debug value(0)
  Debug value(1)
  Debug value(2)
Else
  Debug "Array not declared"
EndIf
PB help => ArraySize() wrote:If the array isn't yet declared (or its allocation has failed), it will return -1.
The problem also exists with normal variables:

Code: Select all

EnableExplicit

Procedure test()
  Global value = 10
EndProcedure

;test()

Debug value
It is best to avoid the keyword Global in procedures, because it messes up the compilation process.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by skywalk »

Sicro wrote:It is best to avoid the keyword Global in procedures, because it messes up the compilation process.
No, I use init() procedures that perform this and other memory tasks. The only element I cannot house within an init() procedure are Prototypes.

Code: Select all

EnableExplicit
Macro do1()
  Prototype.i x(a.i,b.i)
EndMacro
Procedure Init()
  #MY_CONST1     = 1
  Global.i gMY10 = 10
  Global Dim MyArray.s(1)
  MyArray(0) = "Hello"
  MyArray(1) = "World"
EndProcedure
Procedure Do2()
  ProcedureReturn gMY10
EndProcedure
Init()
Debug MyArray(0) + MyArray(1)
Debug Do2()
Debug #MY_CONST1
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by Sicro »

skywalk wrote:No, I use init() procedures that perform this and other memory tasks.
What is wrong with using a macro or a comment and main code instead of a procedure as Init()?

Code: Select all

Macro Init()
  Global NewMap settings$()
  ; More init codes
EndMacro
Init()
or

Code: Select all

;- Initialising
Global NewMap settings$()
; More init codes
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by skywalk »

Think dll. :idea:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by mk-soft »

Link DLL´s:
viewtopic.php?p=485833#p485833
Fred wrote:AttachProcess() should be called on linux as well, if not it's a bug. You can also declare list in procedure if needed, just not in the main code.
This is my way for Modules and DLL´s

Code: Select all

DeclareModule MyModule
  Declare DoAny(Index)
EndDeclareModule

Module MyModule
  
  Procedure InitModule()
    Global Dim MyArray(1)
    Global NewList MyList()
    MyArray(0) = 100
  EndProcedure : InitModule()
  
  Procedure DoAny(Index)
    ProcedureReturn MyArray(Index)
  EndProcedure
  
EndModule

Debug MyModule::DoAny(0)
Last edited by mk-soft on Sat May 18, 2019 11:11 am, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: 5.70 LTS x64: IMA using a global array in a procedure

Post by Sicro »

skywalk wrote:Think dll. :idea:
Then use

Code: Select all

Procedure AttachProcess(Instance)
  ; Your init codes
EndProcedure
Then you don't have to worry whether the init codes are executed or not, because the AttachProcess() procedure is executed automatically.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply