static array in structure out of bound error need suppress

Just starting out? Need help? Post your questions and find answers here.
moricode
User
User
Posts: 93
Joined: Thu May 25, 2023 3:55 am

static array in structure out of bound error need suppress

Post by moricode »

as programmer , we have chance to use many tactics ,
a static array in structure is my favourite to create object handles ,
it runs fine as we know the bound of static array and we manage it ,
the compiler let go on release mode , but raise ERROR in debugger mode .

so i have miss the debugger function if i wish to use this tactic in code.

any tips to suppress this type of compiler error msg so i can continue to use debugger?

or this is by design ?


Code: Select all


; In 32bit x86 Purebasic 5.72 compiler
; This code runs fine in release mode , 
; but Give ERROR in Debugger Mode

;[12:53:24] [ERROR] Line: 52
;[12:53:24] [ERROR] Structure Array index out of bounds.
;[12:53:29] The Program was killed.

; -----------------------------------------------------

Structure dynaobject
   name$
   style.l
   
   
   StructureUnion
      show_body.l   
      show_section.l[1]
   EndStructureUnion
   ; ----
   show_Title.l
   show_Header.l
   show_FootBar.l
   show_FloatOBJ.l
   Show_Rec_status.l  ; Only applicable to MODE = 1 , (Table/ Report)   
   
EndStructure


Procedure.i X_CreateMyObject(ntools.l)

   Protected *thisObj.dynaobject = AllocateMemory(SizeOf(dynaobject))
   
;   Protected *thisObj.dynaobject = AllocateStructure(dynaobject)
   
   ProcedureReturn *thisObj
EndProcedure



*myObj.dynaobject = X_CreateMyObject(6)



; set value in object
For i = 1 To 6
   *myObj\show_section[i-1] = Random(6000)
Next

OpenConsole("Test Object")


PrintN("Size of object: " +Str(SizeOf(dynaobject)))

For i = 1 To 6
  PrintN("Tools "+Str(i)+" value : "+Str(*myObj\show_section[i-1]))
Next

PrintN("Last value : "+Str(*myObj\show_section[5]))

Input()

infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: static array in structure out of bound error need suppress

Post by infratec »

As a placeholder, try this:

Code: Select all

StructureUnion
      show_body.l   
      show_section.l[0]
   EndStructureUnion
moricode
User
User
Posts: 93
Joined: Thu May 25, 2023 3:55 am

Re: static array in structure out of bound error need suppress

Post by moricode »

infratec wrote: Thu May 25, 2023 6:39 am As a placeholder, try this:

Code: Select all

StructureUnion
      show_body.l   
      show_section.l[0]
   EndStructureUnion
Thanks infratec , it worked in debugger mode , i not aware of the [0] , my bad.

cheers
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: static array in structure out of bound error need suppress

Post by idle »

what Infratec said

index from 0

Code: Select all

Structure dynaobject
   name$
   style.l
   
   
   StructureUnion
      show_body.l   
      show_section.l[0]    ;itterate through items below 
   EndStructureUnion
   ; ----
   show_Title.l
   show_Header.l
   show_FootBar.l
   show_FloatOBJ.l
   Show_Rec_status.l  ; Only applicable to MODE = 1 , (Table/ Report)   
   
EndStructure


Procedure.i X_CreateMyObject(ntools.l)

   Protected *thisObj.dynaobject = AllocateMemory(SizeOf(dynaobject))
   
;   Protected *thisObj.dynaobject = AllocateStructure(dynaobject)
   
   ProcedureReturn *thisObj
EndProcedure



*myObj.dynaobject = X_CreateMyObject(6)



; set value in object
For i = 0 To 5
   *myObj\show_section[i] = Random(6000)
Next

OpenConsole("Test Object")


PrintN("Size of object: " +Str(SizeOf(dynaobject)))

For i = 0 To 5
  PrintN("Tools "+Str(i)+" value : "+Str(*myObj\show_section[i]))
   
Next

PrintN("Last value : " +Str(*myObj\ Show_Rec_status))  ;Show_Rec_status is last value.

Input()


moricode
User
User
Posts: 93
Joined: Thu May 25, 2023 3:55 am

Re: static array in structure out of bound error need suppress

Post by moricode »

idle wrote: Thu May 25, 2023 7:10 am what Infratec said

index from 0

Code: Select all

Structure dynaobject
   name$
   style.l
   
   
   StructureUnion
      show_body.l   
      show_section.l[0]    ;itterate through items below 
   EndStructureUnion
   ; ----
   show_Title.l
   show_Header.l
   show_FootBar.l
   show_FloatOBJ.l
   Show_Rec_status.l  ; Only applicable to MODE = 1 , (Table/ Report)   
   
EndStructure


Procedure.i X_CreateMyObject(ntools.l)

   Protected *thisObj.dynaobject = AllocateMemory(SizeOf(dynaobject))
   
;   Protected *thisObj.dynaobject = AllocateStructure(dynaobject)
   
   ProcedureReturn *thisObj
EndProcedure



*myObj.dynaobject = X_CreateMyObject(6)



; set value in object
For i = 0 To 5
   *myObj\show_section[i] = Random(6000)
Next

OpenConsole("Test Object")


PrintN("Size of object: " +Str(SizeOf(dynaobject)))

For i = 0 To 5
  PrintN("Tools "+Str(i)+" value : "+Str(*myObj\show_section[i]))
   
Next

PrintN("Last value : " +Str(*myObj\ Show_Rec_status))  ;Show_Rec_status is last value.

Input()


yes, thanks idle , i was not careful enough , sorry for my stupid.

cheers
moricode
User
User
Posts: 93
Joined: Thu May 25, 2023 3:55 am

Re: static array in structure out of bound error need suppress

Post by moricode »

Thanks guys.

but , why ?
it is worked in index [0] , but not work in index[1] ,
something in somewhere should need to explain ?
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: static array in structure out of bound error need suppress

Post by idle »

moricode wrote: Thu May 25, 2023 7:18 am Thanks guys.

but , why ?
it is worked in index [0] , but not work in index[1] ,
something in somewhere should need to explain ?
The show_section.l[0] with a 0 index turns it into an access patten to index, it doesn't take any space or get compiled

Code: Select all

 StructureUnion
      show_body.l   
      show_section.l[0]    ;place holder 0 sized to iterate through the fields below 
   EndStructureUnion
   ; ----
   show_Title.l
   show_Header.l
   show_FootBar.l
   show_FloatOBJ.l
   Show_Rec_status.l  ; Only applicable to MODE = 1 , (Table/ Report)  

It's a useful feature is easy to make a mistake.
moricode
User
User
Posts: 93
Joined: Thu May 25, 2023 3:55 am

Re: static array in structure out of bound error need suppress

Post by moricode »

idle wrote: Thu May 25, 2023 7:30 am
moricode wrote: Thu May 25, 2023 7:18 am Thanks guys.

but , why ?
it is worked in index [0] , but not work in index[1] ,
something in somewhere should need to explain ?
The show_section.l[0] with a 0 index turns it into an access patten to index, it doesn't take any space or get compiled

Code: Select all

 StructureUnion
      show_body.l   
      show_section.l[0]    ;place holder 0 sized to iterate through the fields below 
   EndStructureUnion
   ; ----
   show_Title.l
   show_Header.l
   show_FootBar.l
   show_FloatOBJ.l
   Show_Rec_status.l  ; Only applicable to MODE = 1 , (Table/ Report)  

It's a useful feature is easy to make a mistake.

yes , thanks again idle.

now i understand , the [0] doesn't get compiled , so it is not exist to compiler , so no error.

cheers.
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: static array in structure out of bound error need suppress

Post by Fred »

Don't worry, it's not that obvious. It's by design, when using 0 as declaration, the debugger is disabled as it will be used for unknown size indexing. I checked the doc and it's not documented, so it needs to be added.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: static array in structure out of bound error need suppress

Post by idle »

Fred wrote: Thu May 25, 2023 8:33 am Don't worry, it's not that obvious. It's by design, when using 0 as declaration, the debugger is disabled as it will be for unknown size indexing. I checked the doc and it's not documented, so it needs to be added.
You know we never mentioned it in case it wasn't by design! shhhh! :lol:
moricode
User
User
Posts: 93
Joined: Thu May 25, 2023 3:55 am

Re: static array in structure out of bound error need suppress

Post by moricode »

ok, well.

If index [0] is by design , then the above code example structure no need the structure union :

Code: Select all

Structure dynaobject
   name$
   style.l
   
   
   show_section.l[0]
   show_body.l   
   ; ----
   show_Title.l
   show_Header.l
   show_FootBar.l
   show_FloatOBJ.l
   Show_Rec_status.l  ; Only applicable to MODE = 1 , (Table/ Report)   
   
EndStructure


Procedure.i X_CreateMyObject()

   Protected *thisObj.dynaobject = AllocateMemory(SizeOf(dynaobject))
   
   ProcedureReturn *thisObj
EndProcedure



*myObj.dynaobject = X_CreateMyObject()



; set value in object
For i = 1 To 6
   *myObj\show_section[i-1] = Random(6000)
Next

OpenConsole("Test Object")


PrintN("Size of object: " +Str(SizeOf(dynaobject)))

For i = 1 To 6
  PrintN("Tools "+Str(i)+" value : "+Str(*myObj\show_section[i-1]))
Next

PrintN("Last value : "+Str(*myObj\show_section[5]))

Input()
moricode
User
User
Posts: 93
Joined: Thu May 25, 2023 3:55 am

Re: static array in structure out of bound error need suppress

Post by moricode »

i am amazed by the magic of index [0] :

Code: Select all

Structure staticArray
   idx.l[0]   
EndStructure


Procedure.i X_staticArray(item)

   Protected *thisObj.staticArray = AllocateMemory(item*4)
   
   ProcedureReturn *thisObj
EndProcedure

*myObj.staticArray = X_staticArray(6)

; set value in object
For i = 1 To 6
   *myObj\idx[i-1] = Random(6000)
Next

OpenConsole("Test Object")

PrintN("Size of Structure : " +Str(SizeOf(staticArray)))

For i = 1 To 6
  PrintN("Tools "+Str(i)+" value : "+Str(*myObj\idx[i-1]))
Next

PrintN("Last value : "+Str(*myObj\idx[5]))

Input()
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: static array in structure out of bound error need suppress

Post by idle »

yes its good but it only does one dimension.
moricode
User
User
Posts: 93
Joined: Thu May 25, 2023 3:55 am

Re: static array in structure out of bound error need suppress

Post by moricode »

even more , Use Datasection as hard-coded static array , access directly without using ReadData() command

oh men, this will be my new chapter in purebasic , i like it .

Code: Select all

; Use datasection as hard-coded static array , access directly without using ReadData() 

DataSection
   BlockSizeArray:
   Data.l 746,517,695,216,814,371,476,816,662,486   ; 5 block x 19 row = 95 block
   Data.l 521,363,242,191,493,748,166,340,163,308
   Data.l 923,470,455,460,299,362,295,331,474,377
   Data.l 180,204,170,170,661,508,200,200,531,325
   Data.l 373,279,555,370,200,200,230,375,792,428
   Data.l 433,210,223,285,273,363,161,505,335,155
   Data.l 457,482,628,597,442,507,150,418,429,383
   Data.l 200,346,460,115,460, 68,313,338,595,480
   Data.l 623,478,457,150,489,489,271,204,805,550
   Data.l 300,367,209,165,228,193,558,495,606,542
   Data.l 580,428,628,475,640,294,520,429,600,437
   Data.l 270,230,418,310,505,347,372,202,317,248
   Data.l 527,316,316,379,436,228,419,419,423,351
   Data.l 288,288,311,255,525,516,509,440,348,450
   Data.l 338,575,211,481,290,516,264,268,546,266
   Data.l 802,636,417,283,330,325,315,241,270,167
   Data.l 715,306,295,185,732,317,190,286,422,500
   Data.l 478,307,211,214,582,229,872,611,656,232
   Data.l 666,478,710,545,294,404,538,315,561,584
         
EndDataSection


Structure dataArray
   idx.l[0]   
EndStructure

*prgData.dataArray = ?BlockSizeArray

OpenConsole("Hard code array")

For i = 0 To 19
   PrintN("Data "+ Str(i+1)+" : "+Str(*prgData\idx[i]))
Next

Input()
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: static array in structure out of bound error need suppress

Post by idle »

Yes it handy and you can write to the array as well.
Post Reply