Differenze tra le versioni di "Malloc ()"

Da Gambas-it.org - Wikipedia.
Riga 12: Riga 12:
 
   
 
   
 
  <FONT color=Gray>' ''void *malloc (size_t size)''
 
  <FONT color=Gray>' ''void *malloc (size_t size)''
  ' ''Allocates the requested memory and returns a pointer to it.''</font>
+
  ' ''Allocate SIZE bytes of memory.''</font>
  Private <FONT color=#B22222>Extern malloc</font>(size As Integer) As Integer
+
  Private Extern <FONT color=#B22222>malloc</font>(size As Long) As Integer
 
   
 
   
  <FONT color=Gray>' ''void free(void* pointer)''
+
  <FONT color=Gray>' ''void free(void *__ptr)''
  ' ''Deallocates the memory previously allocated by a call to malloc.''</font>
+
  ' ''Free a block allocated by `malloc', `realloc' or `calloc'.''</font>
  Private Extern free_C(po As Pointer) Exec "free"
+
  Private Extern free_C(__ptr As Pointer) Exec "free"
 
   
 
   
 
   
 
   
Riga 25: Riga 25:
 
    
 
    
 
   p = <FONT color=#B22222>malloc(16)</font>
 
   p = <FONT color=#B22222>malloc(16)</font>
   If IsNull(p) Then Error.Raise("Impossibile allocare un'area di memoria !")
+
   If p = 0 Then Error.Raise("Impossibile allocare un'area di memoria !")
 
      
 
      
 
  <FONT color=Gray>' ''Liberiamo l'area di memoria appena prima allocata:''</font>
 
  <FONT color=Gray>' ''Liberiamo l'area di memoria appena prima allocata:''</font>

Versione delle 20:05, 9 mar 2016

La funzione della libreria di C

void *malloc (size_t size)

alloca un'area di memoria per uso generico.


Volendola utilizzare in Gambas, bisognerà dichiararla con Extern, nonché bisognerà dichiarare la libreria di C: libc.so.6, nella quale la funzione è contenuta:

Private Extern malloc(size As Integer) As Pointer In "libc:6"


Semplice esempio di uso in Gambas:

Library "libc:6"

' void *malloc (size_t size)
' Allocate SIZE bytes of memory.
Private Extern malloc(size As Long) As Integer

' void free(void *__ptr)
' Free a block allocated by `malloc', `realloc' or `calloc'.
Private Extern free_C(__ptr As Pointer) Exec "free"


Public Sub Main()
 
 Dim p As Pointer
 
  p = malloc(16)
  If p = 0 Then Error.Raise("Impossibile allocare un'area di memoria !")
   
' Liberiamo l'area di memoria appena prima allocata:
  free_C(p)
   
End