Inconsistency with static arrays

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 552
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Inconsistency with static arrays

Post by Joubarbe »

Please correct me if I’m wrong, but in some scenarios, an error should be raised when dealing with static arrays.

Code: Select all

Structure myStruc
  myArray.i[0]
EndStructure

Define a.myStruc
a\myArray[2] = 24

Debug a\myArray[2] ; Outputs 24.
No error. As expected, if you change the structure definition from myArray.i[0] to myArray.i[2], then you have an index out of bounds error. The thing is, with the code above, I don’t really understand where my value of 24 is stored; isn’t it a memory/security problem?

I know it’s not a big deal, so my apologies if it’s a waste of time.
acreis
Enthusiast
Enthusiast
Posts: 182
Joined: Fri Jun 01, 2012 12:20 am

Re: [5.73] Inconsistency with static arrays

Post by acreis »

I think this is by design, so structure can be used so walk along buffer, see this example:

Code: Select all


  ;coud be any type like Double Float Integer Quad or any Structure
   Structure LPSTR
     c.Char[0]
   EndStructure

   Procedure FindStringLike(*String.LPSTR, Mask$)
      
      ;Access n th character of string with *String\c[n] 
      
      
      If Mask$ = "#" and *String\c[1] = "9" then
        Debug "Find:" = *String\c[1]
      EndIf  
      
      
   EndProcedure
   
   String$ = "9999999"
   
   FindStringLike(@String$, "#")
   
User avatar
STARGÅTE
Addict
Addict
Posts: 2063
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: [5.73] Inconsistency with static arrays

Post by STARGÅTE »

PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [5.73] Inconsistency with static arrays

Post by mk-soft »

This is also used a lot to access arrays of unknown size.

Small example ...

Code: Select all

Structure ArrayOfInteger
  iVal.i[0]
EndStructure

Define *a.ArrayOfInteger

Debug "Mydata"
*a = ?MyData
c = *a\iVal[0] 
For i = 1 To c
  Debug *a\iVal[i]
Next

Debug "MyData2"
*a = ?MyData2
c = *a\iVal[0] 
For i = 1 To c
  Debug *a\iVal[i]
Next

DataSection
  MyData:
  Data.i 5
  Data.i 100, 200, 300, 400, 500
  MyData2:
  Data.i 6
  Data.i 1000, 2000, 3000, 4000, 5000, 6000
  
EndDataSection
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
Post Reply