Array index out of bounds (array in a structure)

Just starting out? Need help? Post your questions and find answers here.
Harry0
User
User
Posts: 13
Joined: Sun Mar 02, 2008 4:28 pm
Location: Palatine

Array index out of bounds (array in a structure)

Post by Harry0 »

I am getting this error = (Array index out of bounds) when I try to set an array element that is with-in a structure.
Sample code below:

Code: Select all

QDN.i = 5

Structure Date_Items
  DOE.l
  DOW.b
  DOM.b
  DOY.w
  WOY.b
  MOY.b
EndStructure

Structure Base_Record
  DI.Date_Items
  Array Ball_G1.b(QDN)
  Ball_G2.b
  Array Hits_G1.w(QDN)
  Hits_G2.w
  Array Freq_G1.f(QDN)
  Freq_G2.f
  ESN.i
  GameName.i
EndStructure

Global NewList Game.base_record()

AddElement(Game())
If Game() <> 0

  REC_Counter = REC_Counter + 1
  Field_Counter = Field_Counter + 1
  Debug REC_Counter
  Debug Field_Counter
  
  With Game()
    \Ball_G2 = 0
    \Hits_G2 = 0
    \DI\DOE = 0
    \DI\DOM = 0
    \DI\DOW = 0
    \DI\DOY = 0
    \DI\MOY = 0
    \DI\WOY = 0
    \GameName = 0
    For x = 0 To QDN
      Debug x
      Debug QDN
      \Ball_G1(x) = 0  ; <- get error here = Array index out of bounds
      \Hits_G1(x) = 0
    Next x
  EndWith
EndIf
I am using PB 4.51 on a windows 7 - 64bit system with 8GB of memory
I have spent 3 days tracking this down to just this bit of code.
The really odd part is that if I disable the debugger, it works here but my main program just hangs (think it is a different issue for the hang).

I have tried using the search function in the forum with no luck.
What am I missing or doing wrong?
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: Array index out of bounds (array in a structure)

Post by Guimauve »

Try this

Code: Select all

#QDN = 10

Structure Date_Items
  DOE.l
  DOW.b
  DOM.b
  DOY.w
  WOY.b
  MOY.b
EndStructure

Structure Base_Record
  
  QDN.l
  DI.Date_Items
  Array Ball_G1.b(0)
  Ball_G2.b
  Array Hits_G1.w(0)
  Hits_G2.w
  Array Freq_G1.f(0)
  Freq_G2.f
  ESN.i
  GameName.i
  
EndStructure


Procedure InitializeBaseRecordArrays(*Base_RecordA.Base_Record, QDN.l)
  
  *Base_RecordA\QDN = QDN
  
  ReDim *Base_RecordA\Ball_G1(*Base_RecordA\QDN)
  ReDim *Base_RecordA\Hits_G1(*Base_RecordA\QDN)
  ReDim *Base_RecordA\Freq_G1(*Base_RecordA\QDN)
  
EndProcedure 



Global NewList Game.Base_Record()

AddElement(Game())
If Game() <> 0
  
  REC_Counter = REC_Counter + 1
  Field_Counter = Field_Counter + 1
  Debug REC_Counter
  Debug Field_Counter
  Debug ""
  InitializeBaseRecordArrays(Game(), #QDN)
  
  With Game()
    \Ball_G2 = 0
    \Hits_G2 = 0
    \DI\DOE = 0
    \DI\DOM = 0
    \DI\DOW = 0
    \DI\DOY = 0
    \DI\MOY = 0
    \DI\WOY = 0
    \GameName = 0
    For x = 0 To \QDN
      Debug x
      Debug \QDN
      Debug ""
      \Ball_G1(x) = 0  ; <- get error here = Array index out of bounds
      \Hits_G1(x) = 0
    Next x
  EndWith
EndIf
Best Regards
Guimauve
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Array index out of bounds (array in a structure)

Post by STARGÅTE »

The problem is that a structure is created to compiler times in which a variable is not defined yet.

If you use a constant #QDN = 5, the arrays initiallisiert automatically, with the size 5, a ReDim So then unnecessary.

Code: Select all

#QDN = 5

Structure Date_Items
  DOE.l
  DOW.b
  DOM.b
  DOY.w
  WOY.b
  MOY.b
EndStructure

Structure Base_Record
  DI.Date_Items
  Array Ball_G1.b(#QDN)
  Ball_G2.b
  Array Hits_G1.w(#QDN)
  Hits_G2.w
  Array Freq_G1.f(#QDN)
  Freq_G2.f
  ESN.i
  GameName.i
EndStructure

Global NewList Game.base_record()

AddElement(Game())
If Game() <> 0

  REC_Counter = REC_Counter + 1
  Field_Counter = Field_Counter + 1
  Debug REC_Counter
  Debug Field_Counter
  
  With Game()
    \Ball_G2 = 0
    \Hits_G2 = 0
    \DI\DOE = 0
    \DI\DOM = 0
    \DI\DOW = 0
    \DI\DOY = 0
    \DI\MOY = 0
    \DI\WOY = 0
    \GameName = 0
    For x = 0 To #QDN
      Debug x
      Debug #QDN
      \Ball_G1(x) = 0  ; <- get error here = Array index out of bounds
      \Hits_G1(x) = 0
    Next x
  EndWith
EndIf
if the array sizes not always the same, just use the method of Guimauve with start array size 0 and then ReDim it on your current element.
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
Harry0
User
User
Posts: 13
Joined: Sun Mar 02, 2008 4:28 pm
Location: Palatine

Re: Array index out of bounds (array in a structure)

Post by Harry0 »

The explaination doesn't make sense.
If it works with a constant then why not a variable as both are defined (and set) before the structure/array is defined.
And why would the ReDim work with a variable and not a Dim?

Also, I found the the issue with my hang and it was not related to this issue.

If what you say is correct then this should not work (try it with and without the debugger on):

Code: Select all

QDN.i = 5

Structure Date_Items
  DOE.l
  DOW.b
  DOM.b
  DOY.w
  WOY.b
  MOY.b
EndStructure

Structure Base_Record
  DI.Date_Items
  Array Ball_G1.b(QDN)
  Ball_G2.b
  Array Hits_G1.w(QDN)
  Hits_G2.w
  Array Freq_G1.f(QDN)
  Freq_G2.f
  ESN.i
  GameName.i
EndStructure

Global NewList Game.base_record()

AddElement(Game())
If Game() <> 0

  REC_Counter = REC_Counter + 1
  Field_Counter = Field_Counter + 1
  Debug REC_Counter
  Debug Field_Counter
  
  With Game()
    \Ball_G2 = 0
    \Hits_G2 = 0
    \DI\DOE = 0
    \DI\DOM = 0
    \DI\DOW = 0
    \DI\DOY = 0
    \DI\MOY = 0
    \DI\WOY = 0
    \GameName = 0
    For x = 0 To QDN
      Debug x
      Debug QDN
      \Ball_G1(x) = x  ; <- get error here = Array index out of bounds
      \Hits_G1(x) = x
    Next x
  EndWith
EndIf

OpenConsole()
ConsoleTitle ("Test:")
PrintN("")
PrintN("Runing in Batch Mode")

FirstElement(Game())
With Game()
  PrintN("values of elements:")
  For x = 0 To QDN
    PrintN("\Ball_G1(x) = " + Str(\Ball_G1(x)))
    PrintN("\Hits_G1(x) = " + Str(\Hits_G1(x)))
  Next x
  PrintN("End of elements")
EndWith
PrintN("Hit return or enter to end")
Input()
   
I am beginning to believe this is a bug (or inconsistent behavior)?
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Array index out of bounds (array in a structure)

Post by Demivec »

Here is a small change that should make it a little more understandable for you. Change your loop starting in line #44 of your last code sample to include these two Debug statements at the beginning of the loop:

Code: Select all

 Debug ArraySize(\Ball_G1())
Debug ArraySize(\Hits_G1())
This will show you that the arrays are defined with no initial size. This is because you used a variable when specifying the arrays' initial dimension in the structure definition. That should have logically raised a compile error, but it doesn't.

As STARGÅTE mentioned, a variable's value isn't defined until run-time. The structure needs to be defined at compile-time and thus can only use constants or literals in it's definition.

You can use either Dim or ReDimm to change the dynamic arrays' size in the structure at runtime.
Post Reply