cambia il nome della funzione in NewString, cosi' al chiamante è chiaro che deve rilasciarla; alloca può essere fuorviante, infatti in alcuni SO ci sono delle API che si chiamano alloc e che liberano automaticamente all'uscita dei blocchi.Originariamente inviato da Fingard
Sempre sulle malloc, mi sta venendo un dubbio parecchio atroce:
char* alloca (int n){
char* stringa = malloc (n * sizeof(char));
return stringa;
}
Dato che ci sei, siccome hai una NewString, crea anche una FreeStringIf del tipo:
che puoi ad esempio utilizzare così:codice:char * FreeStringIf (char * theString){ if (NULL != theString){ free (theString); } return NULL; }
codice:int main (void){ char * aString = NewString (1024); if (NULL != aString){ // your program is handling the good string.. } aString = FreeStringIf (aString); return 0; }

Rispondi quotando