It is currently Sun Jul 21, 2019 8:19 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Thu May 16, 2019 9:06 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 11, 2006 12:07 am
Posts: 488
Location: Germany / one of the fishheads
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:
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 5.62, OS: Windows 7 Pro x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age: 51y
"Happiness is a pet." | "Never run a changing system!"


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Thu May 16, 2019 9:13 am 
Offline
Moderator
Moderator
User avatar

Joined: Thu Dec 31, 2009 11:05 pm
Posts: 1034
Location: Germany::Berlin()
I can confirm that.

_________________
ImageImageImageImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Thu May 16, 2019 9:04 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1769
Location: Germany
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 / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Thu May 16, 2019 9:09 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 11, 2006 12:07 am
Posts: 488
Location: Germany / one of the fishheads
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 5.62, OS: Windows 7 Pro x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age: 51y
"Happiness is a pet." | "Never run a changing system!"


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Thu May 16, 2019 9:29 pm 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1008
Location: right here
hehe :) I always thought you can't define a zero-size array. apparently it's possible (without ExtractJSON/XMLArray). 8)


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Thu May 16, 2019 9:41 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 11, 2006 12:07 am
Posts: 488
Location: Germany / one of the fishheads
@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 5.62, OS: Windows 7 Pro x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age: 51y
"Happiness is a pet." | "Never run a changing system!"


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Thu May 16, 2019 9:41 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1769
Location: Germany
Empty Array...
Code:
EnableExplicit

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

Debug ArraySize(MyArray())

InitArray(100)

Debug ArraySize(MyArray())

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Thu May 16, 2019 10:08 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Apr 18, 2019 8:17 am
Posts: 202
Code:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Thu May 16, 2019 10:15 pm 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1008
Location: right here
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Fri May 17, 2019 1:44 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 343
Location: Germany
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:
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:
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
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Fri May 17, 2019 3:04 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 2969
Location: Boston, MA
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Fri May 17, 2019 10:38 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 343
Location: Germany
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:
Macro Init()
  Global NewMap settings$()
  ; More init codes
EndMacro
Init()
or
Code:
;- Initialising
Global NewMap settings$()
; More init codes

_________________
Image
Why OpenSource should have a license
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Sat May 18, 2019 3:24 am 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 2969
Location: Boston, MA
Think dll. :idea:

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Sat May 18, 2019 11:03 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1769
Location: Germany
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:
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)

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Last edited by mk-soft on Sat May 18, 2019 11:11 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: 5.70 LTS x64: IMA using a global array in a procedure
PostPosted: Sat May 18, 2019 11:07 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 343
Location: Germany
skywalk wrote:
Think dll. :idea:
Then use
Code:
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
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye