Differenze tra le versioni di "Conoscere la quantità di memoria occupata dai tipi di dati di C e la loro locazione nei file header"
Da Gambas-it.org - Wikipedia.
Riga 1: | Riga 1: | ||
− | Per | + | Per conoscere la quantità di memoria occupata da un tipo di valore, definito in linguaggio C, ed in quale file header di sistema esso è dichiarato, possiamo utilizzare il seguente codice: |
<FONT Color=gray>' ''long Dim_Tipo(void)'' | <FONT Color=gray>' ''long Dim_Tipo(void)'' | ||
' ''Restituisce la quantità di memoria occupata dal tipo di valore cercato.''</font> | ' ''Restituisce la quantità di memoria occupata dal tipo di valore cercato.''</font> | ||
Riga 7: | Riga 7: | ||
'''Public''' Sub Main() | '''Public''' Sub Main() | ||
− | Dim header As String[] = ["/usr/include/x86_64-linux-gnu/bits/types.h", | + | Dim header As String[] = ["/usr/include/x86_64-linux-gnu/bits/types.h", <FONT Color=gray>' ''i file header delle librerie ove ad esempio cercare''</font> |
"/usr/include/x86_64-linux-gnu/sys/types.h", | "/usr/include/x86_64-linux-gnu/sys/types.h", | ||
− | "/usr/include/linux/types.h", | + | "/usr/include/linux/types.h", "/usr/include/stdint.h", |
− | "/usr/include/ | + | "/usr/include/unistd.h"] |
Dim perc, s, tipo, ex As String | Dim perc, s, tipo, ex As String | ||
Dim ss As String[] | Dim ss As String[] | ||
Dim sh As Short | Dim sh As Short | ||
Dim d As Byte | Dim d As Byte | ||
− | + | ||
− | + | <FONT Color=gray>' ''Cerchiamo un tipo valore:''</font> | |
− | + | tipo = "<FONT Color=#B22222>u_int</font>" | |
− | + | ||
− | + | Print "Inizio ricerca:\n\n" | |
− | + | ||
− | + | For Each perc In header | |
− | + | s = File.Load(perc) | |
− | + | ss = Split(s, "\x0A") | |
− | + | sh = ss.Find("typedef " & tipo & " *", gb.Like) | |
− | + | If sh > -1 Then | |
− | + | Print "\n " & ss[sh] | |
− | + | Print " trovato nel file header: '\e[34m" & perc & "\e[0m' - riga: "; sh + 1 | |
− | + | Endif | |
− | + | sh = ss.Find("* " & tipo & ";", gb.Like) | |
− | + | If sh > -1 Then | |
− | + | Print "\n " & ss[sh] | |
− | + | Print " trovato nel file header: '\e[34m" & perc & "\e[0m' - riga: "; sh + 1 | |
− | + | Endif | |
− | + | Next | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | d = CByte(DimensioneTipo(tipo)) | ||
+ | Print "\nIl tipo di dato '" & tipo & "' occupa [ "; d; " ] byte di memoria." | ||
+ | |||
+ | Print "\n\e[31mFine ricerca" | ||
+ | |||
'''End''' | '''End''' | ||
+ | '''Private''' Function DimensioneTipo(tp As String) As Long | ||
− | |||
− | |||
File.Save("/tmp/libTipo.c", "#include <bits/types.h>" & | File.Save("/tmp/libTipo.c", "#include <bits/types.h>" & | ||
"\n#include <sys/types.h>" & | "\n#include <sys/types.h>" & | ||
"\n#include <linux/types.h>" & | "\n#include <linux/types.h>" & | ||
"\n#include <stdint.h>" & | "\n#include <stdint.h>" & | ||
+ | "\n#include <unistd.h>" & | ||
"\n\nlong Dim_Tipo() {" & | "\n\nlong Dim_Tipo() {" & | ||
"\n\n return sizeof(" & tp & ");" & | "\n\n return sizeof(" & tp & ");" & | ||
"\n}") | "\n}") | ||
− | + | ||
Shell "gcc -o /tmp/libTipo.so /tmp/libTipo.c -shared -fPIC" Wait | Shell "gcc -o /tmp/libTipo.so /tmp/libTipo.c -shared -fPIC" Wait | ||
− | + | ||
Return Dim_Tipo() | Return Dim_Tipo() | ||
− | + | ||
'''End''' | '''End''' |
Versione attuale delle 11:49, 11 set 2022
Per conoscere la quantità di memoria occupata da un tipo di valore, definito in linguaggio C, ed in quale file header di sistema esso è dichiarato, possiamo utilizzare il seguente codice:
' long Dim_Tipo(void) ' Restituisce la quantità di memoria occupata dal tipo di valore cercato. Private Extern Dim_Tipo() As Long In "/tmp/libTipo" Public Sub Main() Dim header As String[] = ["/usr/include/x86_64-linux-gnu/bits/types.h", ' i file header delle librerie ove ad esempio cercare "/usr/include/x86_64-linux-gnu/sys/types.h", "/usr/include/linux/types.h", "/usr/include/stdint.h", "/usr/include/unistd.h"] Dim perc, s, tipo, ex As String Dim ss As String[] Dim sh As Short Dim d As Byte ' Cerchiamo un tipo valore: tipo = "u_int" Print "Inizio ricerca:\n\n" For Each perc In header s = File.Load(perc) ss = Split(s, "\x0A") sh = ss.Find("typedef " & tipo & " *", gb.Like) If sh > -1 Then Print "\n " & ss[sh] Print " trovato nel file header: '\e[34m" & perc & "\e[0m' - riga: "; sh + 1 Endif sh = ss.Find("* " & tipo & ";", gb.Like) If sh > -1 Then Print "\n " & ss[sh] Print " trovato nel file header: '\e[34m" & perc & "\e[0m' - riga: "; sh + 1 Endif Next d = CByte(DimensioneTipo(tipo)) Print "\nIl tipo di dato '" & tipo & "' occupa [ "; d; " ] byte di memoria." Print "\n\e[31mFine ricerca" End Private Function DimensioneTipo(tp As String) As Long File.Save("/tmp/libTipo.c", "#include <bits/types.h>" & "\n#include <sys/types.h>" & "\n#include <linux/types.h>" & "\n#include <stdint.h>" & "\n#include <unistd.h>" & "\n\nlong Dim_Tipo() {" & "\n\n return sizeof(" & tp & ");" & "\n}") Shell "gcc -o /tmp/libTipo.so /tmp/libTipo.c -shared -fPIC" Wait Return Dim_Tipo() End