View Full Version : C: Funzione che restituisce la memoria libera??
E' tutto il pomeriggio che cerco una funzione in C che mi restituisce la memoria libera :muro: . Qualcuno mi può aiutare ;)
ilsensine
13-02-2003, 16:46
Non esiste una funzione standard, e la procedura dipende dal sistema operativo. Sotto windows dovrebbe esserci una API che ora ovviamente non ricordo.
Per Windows :
// Sample output:
// c:\>globalex
// 78 percent of memory is in use.
// There are 65076 total Kbytes of physical memory.
// There are 14248 free Kbytes of physical memory.
// There are 150960 total Kbytes of paging file.
// There are 88360 free Kbytes of paging file.
// There are 1fff80 total Kbytes of virtual memory.
// There are 1fe770 free Kbytes of virtual memory.
// There are 0 free Kbytes of extended memory.
#define _WIN32_WINNT 0x0500
#include <windows.h>
// Use to change the divisor from Kb to Mb.
#define DIV 1024
// #define DIV 1
char *divisor = "K";
// char *divisor = "";
// Handle the width of the field in which to print numbers this way to
// make changes easier. The asterisk in the print format specifier
// "%*I64d" takes an int from the argument list, and uses it to pad
// and right-justify the number being formatted.
#define WIDTH 7
void main(int argc, char *argv[])
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
printf ("%ld percent of memory is in use.\n",
statex.dwMemoryLoad);
printf ("There are %*I64d total %sbytes of physical memory.\n",
WIDTH, statex.ullTotalPhys/DIV, divisor);
printf ("There are %*I64d free %sbytes of physical memory.\n",
WIDTH, statex.ullAvailPhys/DIV, divisor);
printf ("There are %*I64d total %sbytes of paging file.\n",
WIDTH, statex.ullTotalPageFile/DIV, divisor);
printf ("There are %*I64d free %sbytes of paging file.\n",
WIDTH, statex.ullAvailPageFile/DIV, divisor);
printf ("There are %*I64x total %sbytes of virtual memory.\n",
WIDTH, statex.ullTotalVirtual/DIV, divisor);
printf ("There are %*I64x free %sbytes of virtual memory.\n",
WIDTH, statex.ullAvailVirtual/DIV, divisor);
// Show the amount of extended memory available.
printf ("There are %*I64x free %sbytes of extended memory.\n",
WIDTH, statex.ullAvailExtendedVirtual/DIV, divisor);
}
Requirements
Windows NT/2000 or later: Requires Windows 2000 or later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Non c'è la funzione malloc? Della stlib?
Non ricordo molto bene come funge. E' quasi un anno che ho passato quell'esame!
malloc serve per allocare la memoria...non per vedere quanta ce n'è libera...
Intanto grazie a tutti.
Mi scuso per la domanda troppo generica però credevo esistesse una funzione std per risolvere il mio problema.
Il programma deve funzionare sotto dos ed utilizza solo memoria convenzionale. Spero possate aiutarmi
Grazie
Hai provato a cercare nell'help ?
memfree, freemem, memused o qualcosa del genere ?
Sì, ma non ho trovato nulla. Se vado avanti così entro stasera mi sono letto tutto l'help :eek:
Niente. L'unica cosa che ho trovato è biosmemory che restituisce la memoria totale
Bah, riguardando gli appunti ho usato una cosa della stlib che si chiama free, restituisce la memoria allocata con malloc mi pare, solo che non se ti è utile. (non sono un buon programmatore)!
Ciao!
Originally posted by "dado82"
Bah, riguardando gli appunti ho usato una cosa della stlib che si chiama free, restituisce la memoria allocata con malloc mi pare, solo che non se ti è utile. (non sono un buon programmatore)!
free libera la memoria allocata con malloc, ma non rende la memoria disponibile sul sistema...
Comunque ora che ci penso se il tuo programma è per DOS (e non per Windows) potrai avere al massimo 640Kb di memoria libera...quindi è unitile anche cercare quella funzione...
Originally posted by "ilsensine"
Non esiste una funzione standard, e la procedura dipende dal sistema operativo. Sotto windows dovrebbe esserci una API che ora ovviamente non ricordo.
Che ti pagano per avere in sign il link a fox?
La funzione mi serve per essere sicuro di rilasciare i puntatori in maniera corretta così da non andare a sovrascrivere parti di memoria già occupate (l'applicazione potrebbe funzionare diversi mesi senza essere rilanciata).
Se il mio dubbio è sbagliato, vi prego di corregermi visto che non conosco molto beneil C ma devo, ale volte, programmarci per lavoro. Grazie
ilsensine
17-02-2003, 08:40
Originally posted by "mjordan"
Che ti pagano per avere in sign il link a fox?
Nulla, le fox sono software libero :rolleyes:
Originally posted by "ilsensine"
Nulla, le fox sono software libero :rolleyes:
Lo so che è software libero. Datti una rilassata. Non si può neanche scherzare?
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.