Autore Topic: Giochiamo con memoria allocata, Puntatori e Memory Stream  (Letto 216 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.309
  • Ne mors quidem nos iunget
    • Mostra profilo
Giochiamo con memoria allocata, Puntatori e Memory Stream
« il: 05 Dicembre 2016, 03:11:39 »
Vi presento un semplicissimo codice con scopo meramente didattico, che intende mostrare come va impostato un ipotetico codice Gambas in caso di allocazione di un'area di memoria, puntata da una variabile di tipo Puntatore, nella quale scrivere con i Memory Stream uno o più dati.

Il programmino mostra:
* due TableView, che rappresentano l'area di memoria allocata nella quale inserire uno più valori di tipo Byte ed il numero d'indice di ciascuna cella della memoria allocata. Ogni cella della TableView rappresenta una cella della memoria allocata. In alto è indicato, come già detto, il numero d'indice per ciascuna cella di memoria.
* una TextArea che mostra il conseguente codice che dovrebbe essere scritto per ottenere la quantità prescelta di memoria da allocare ed i valori che si vuole vadano in essa scritti.

All'avvio verrà richiesta la quantità di memoria (da 1 a 16) da riservare.
Per inserire i valori di tipo Byte, bisognerà cliccare in una cella, scrivere quindi il valore di tipo Byte (da 0 a 255), ed infine  premere il tasto "Invio" della tastiera.
E' possibile riscrivere un valore in una cella, che sia stata già valorizzata.

Codice: [Seleziona]
Private tv As TableView
Private ta As TextArea
Private chd As String
Private primus As Boolean
Private inx As Short = -1


Public Sub Form_Open()

  Dim b As Byte
  Dim index As TableView
   
    Do
      b = Val(InputBox("Inserisci la quantità di memoria da allocare<BR>(min 1, max 16):", Null, "8"))
    Loop Until (b > 0) And (b < 17)
   
    With index = New TableView(Me)
      .X = 20
      .Y = 40
      .W = 500
      .H = 20
      .Rows.Count = 1
      .Rows.Height = index.H
      .Columns.Count = b
      .ScrollBar = 0
    End With
    For b = 0 To index.Columns.Count - 1
      With index
        .Columns.W = .W \ .Columns.Count
        .Columns[b].Alignment = Align.Center
        index[0, b].Text = CStr(b)
      End With
    Next
   
    With tv = New TableView(Me) As "TableView"
      .X = 20
      .Y = index.Y + index.H + 5
      .W = 500
      .H = 50
      .Rows.Count = 1
      .Rows.Height = tv.H
      .Columns.Count = b
      .ScrollBar = 0
    End With
   
    For b = 0 To tv.Columns.Count - 1
      With tv
        .Columns.W = .W \ .Columns.Count
        .Columns[b].Alignment = Align.Center
        tv[0, b].Text = "0"
      End With
    Next
   
    ImpostaTextArea(b)

    With Me
      .Center
      .W = tv.W + 40
      .H = index.H + tv.H + ta.H + 100
    End With
   
    primus = True

End


Public Sub TableView_Click()

  tv.Edit

End


Public Sub TableView_Save(riga As Integer, col As Integer, valore As String)

  Dim sk As String

   If (valore = Null) Or (Len(valore) > 3) Or (Val(valore) > 255) Then Return
   If tv[0, tv.Column].Text = valore Then Return
   If tv.Columns.Count > 1 Then
     If (col > 0) And (tv.Column <> inx + 1) Then sk = "\n\n Seek #st, " & CStr(col)
     If (col = 0) And (primus = False) Then sk = "\n\n Seek #st, " & CStr(col)
   Endif
 
   tv[riga, col].Text = valore
 
   With ta
     .Text = Left(.Text, Len(.Text) - (Len(chd)))
     .Text &= sk & "\n Write #st, " & valore & " As Byte" & chd
   End With
   
   sk = Null
     
   primus = False
   inx = tv.Column

End


Private Procedure ImpostaTextArea(n As Byte)

  chd = "\n\n st.Close\n\n ......\n ......\n\n Free(p)"
 
  With ta = New TextArea(Me)
    .X = 20
    .Y = tv.Y + tv.H + 40
    .W = tv.W
    .H = 300
    .Text = "Dim p As Pointer\nDim st As Stream\n\n" &
            " p = Alloc(SizeOf(gb.byte), " & CStr(n) & ")\n\n" &
            " st = Memory p For Write" & chd
  End With
 
End


« Ultima modifica: 02 Novembre 2021, 02:13:14 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »