Overview: Trying to use structures (as records), do I need to buffer
between element arrays in the structure?
Example program also uses 'CopyMemory' and "File' comands.
Issue is repeatable as example program can show both
modes (working ok & What I think is the variable be corrupted)
Details: Please look at the commented code example.
;constants
;
#Num_Elem.W = 5
;
;Vars & Structures
;
;
; For first test, run as is.
;
; Watch the variable 'V_Txt_Pos',
; it should be equal 100 * 'H_Txt_Pos',
; which is also equal to the value of the
; loop index.
;
; This test should show different numbers in
; the variable 'V_Txt_Pos' and then crash at the
; end (Maybe?!?!?!?).
;
;-------------------------------------------------
;
; for second test, uncomment the line in the
; Strucutres of 'Def_Col' and 'REC_col' to
; enable the variable called 'single_byte.
;
; All should be ok.
;
; At this point, either:
; I am missing something
; I am coding something wrong
; OR
; Is this an issue with PureBasic?
;
x_var.w = 0
y_var.w = 0
Structure Def_Col
HeadLine1.s
HeadLine2.s
Col_Formt.s
Disp_Wdth.w
H_Txt_Pos.w
V_Txt_Pos.w
; single_byte.b ;For second test, uncomment this variable
currency.w[#Num_Elem]
EndStructure
Structure REC_Col
HeadLine1.b[16]
HeadLine2.b[16]
Col_Formt.b[16]
Disp_Wdth.w
H_Txt_Pos.w
V_Txt_Pos.w
; single_byte.b ;For second test, uncomment this variable
currency.w[#Num_Elem]
EndStructure
Dim Test_Var.def_col(#Num_Elem)
Global Test_Rec.rec_col
Tst_File_Name.s = "Test_Code_File.txt"
;
;-------------------------------------
;
;Start Of Defining Procedures
;
;-------------------------------------
;
Procedure Reset_Array(Rec_Num.w)
test_var(Rec_Num)\headline1 = Space(256)
test_var(Rec_Num)\headline2 = Space(256)
test_var(Rec_Num)\Col_Formt = Space(256)
test_var(Rec_Num)\Disp_wdth = 0
test_var(Rec_Num)\H_txt_pos = 0
test_var(Rec_Num)\V_txt_pos = 0
For y_var = 1 To #Num_Elem
test_var(rec_num)\currency[y_var-1] = 0
Next y_var
EndProcedure
Procedure Fill_Array(Rec_Num.w)
test_var(Rec_Num)\headline1 = "Column " + Str(Rec_Num) + " part a."
test_var(Rec_Num)\headline2 = "Column " + Str(Rec_Num) + " part b."
test_var(Rec_Num)\Col_Formt = "Format = " + Str(Rec_Num) + "."
test_var(Rec_Num)\Disp_wdth = Rec_Num*10
test_var(Rec_Num)\H_txt_pos = Rec_Num*1
test_var(Rec_Num)\V_txt_pos = Rec_Num*100
For y_var = 1 To #Num_Elem
test_var(Rec_Num)\currency[y_var-1] = (Rec_Num*100)+y_var
Next y_var
EndProcedure
Procedure Prnt_Array(Rec_Num.w)
PrintN("Printing Record : (element " + Str(Rec_Num) + ")")
PrintN(" ")
PrintN(" " + "Display Heading = " + Trim(test_var(Rec_Num)\headline1))
PrintN(" " + "Display Heading = " + Trim(test_var(Rec_Num)\headline2))
PrintN(" " + "Display Format = " + Trim(test_var(Rec_Num)\Col_Formt))
PrintN(" " + "Display Width = " + Str(test_var(Rec_Num)\disp_wdth))
PrintN(" " + "Display H_Pos = " + Str(test_var(Rec_Num)\H_txt_pos) + " ; NOTE - Loop Index")
PrintN(" " + "Display V_Pos = " + Str(test_var(Rec_Num)\v_txt_pos) + " ; Watch this value")
For Y_var = 1 To #Num_Elem
PrintN(" " + "Display Curr(" + Str(y_var) + ") = " + Str(test_var(rec_num)\currency[y_var-1]))
Next Y_var
EndProcedure
Procedure Xfer2Rec(Rec_Num.w)
CopyMemory(@test_var(Rec_Num)\headline1, @test_rec\headline1[0], 16)
CopyMemory(@test_var(Rec_Num)\headline2, @test_rec\headline2[0], 16)
CopyMemory(@test_var(Rec_Num)\Col_Formt, @test_rec\Col_Formt[0], 16)
test_rec\disp_wdth = test_var(rec_num)\disp_wdth
test_rec\h_txt_pos = test_var(rec_num)\h_txt_pos
test_rec\v_txt_pos = test_var(rec_num)\v_txt_pos
For y_var = 1 To #Num_Elem
test_rec\currency[y_var-1] = test_var(rec_num)\currency[y_var-1]
Next y_var
EndProcedure
Procedure Xfer_4_Rec(Rec_Num.w)
CopyMemory(@test_rec\headline1[0], @test_var(Rec_Num)\headline1, 16)
CopyMemory(@test_rec\headline2[0], @test_var(Rec_Num)\headline2, 16)
CopyMemory(@test_rec\Col_Formt[0], @test_var(Rec_Num)\Col_Formt, 16)
test_var(rec_num)\disp_wdth = test_rec\disp_wdth
test_var(rec_num)\h_txt_pos = test_rec\h_txt_pos
test_var(rec_num)\v_txt_pos = test_rec\v_txt_pos
For y_var = 1 To #Num_Elem
test_var(rec_num)\currency[y_var-1] = test_rec\currency[y_var-1]
Next y_var
EndProcedure
Procedure Debug_Array(Rec_Num.w)
Debug test_var(Rec_Num)\headline1
Debug test_var(Rec_Num)\headline2
Debug test_var(Rec_Num)\Col_Formt
Debug test_var(Rec_Num)\disp_wdth
Debug test_var(Rec_Num)\H_txt_pos
Debug test_var(Rec_Num)\v_txt_pos
For Y_var = 1 To #Num_Elem
Debug test_var(rec_num)\currency[y_var-1]
Next Y_var
EndProcedure
;
;-------------------------------------
;
;Start Of Program
;
;-------------------------------------
;
;CallDebugger
If OpenConsole()
PrintN("Number of elements in each of the arrays = " + Str(#num_elem) + " .")
PrintN("Size of the array (Test_Var) = " + Str(SizeOf(def_col)) + " .")
PrintN("Size of the array (Test_REC) = " + Str(SizeOf(rec_col)) + " .")
PrintN("Reseting the array.")
For x_var = 1 To #Num_Elem
reset_array(x_var)
Next x_var
PrintN("Creating the array.")
For x_var = 1 To #Num_Elem
fill_array(x_var)
; debug_array(x_var)
Next x_var
PrintN("Printing Records before write to the file :")
For x_var = 1 To #Num_Elem
prnt_array(x_var)
PrintN(" ")
PrintN("Press Return for the next set: ")
Input()
PrintN(" ")
Next x_var
If CreateFile(1, Tst_File_Name)
PrintN("Creating the file.")
For x_var = 1 To #Num_Elem
xfer2rec(x_var)
WriteData(@test_rec, SizeOf(Rec_Col))
Next x_var
PrintN("Closing the file.")
CloseFile(1)
Else
PrintN(" ")
PrintN("ERROR - ERROR - Could NOT create the file of: " + tst_file_name + " .")
Input()
End
EndIf
PrintN("Printing Records before read of file :")
For x_var = 1 To #Num_Elem
prnt_array(x_var)
PrintN(" ")
PrintN("Press Return for the next set: ")
Input()
PrintN(" ")
Next x_var
PrintN("Reseting the array.")
For x_var = 1 To #Num_Elem
reset_array(x_var)
Next x_var
PrintN("Opening the file for the next set: ")
PrintN("Reading the file.")
If ReadFile(1,Tst_File_Name)
For x_var = 1 To #Num_Elem
ReadData(@test_rec, SizeOf(Rec_Col))
xfer_4_rec(x_var)
Next x_var
PrintN("Closing the file.")
CloseFile(1)
Else
PrintN(" ")
PrintN("ERROR - ERROR - Could NOT open the file of: " + tst_file_name + " .")
Input()
End
EndIf
PrintN("Printing structure after read of file.")
For x_var = 1 To #Num_Elem
prnt_array(x_var)
PrintN(" ")
PrintN("Press Return for the next set: ")
Input()
PrintN(" ")
Next x_var
PrintN("End of test code: ")
Input()
CloseConsole()
EndIf
End
;----------------------------
Any Questions, please post here and I will respond.
Am I crazy, a bad programmer or is this an issue w/PureBasic?
HarryO
