Salve, ho scritto questo programma:
mi chiedevo, Come buona pratica di programmazione, che controlli dovrei effettuare??codice:#include <stdio.h> #include <stdlib.h> #include <time.h> char* myTime( void ) { struct tm *timeinfo = NULL; time_t rawtime = 0; time_t ver = 0; char *buffer = NULL; buffer = (char *)calloc( 25 , sizeof( char ) ); time( &rawtime ); timeinfo = localtime( &rawtime ); strftime( buffer , 25 , "%d-%m-%Y alle %H:%M:%S" , timeinfo ); return buffer; } /* main alla meno peggio per provare la funzione */ int main() { char *buffer; buffer=myTime(); puts(buffer) ; return 0; }
e' bene controllare input delle funzioni e il valore restituito di ogni singola funzione?
Ad esempio il programma cosi controllato:
codice:char* TimeError( void ) { /* ============ DEFINIZIONE E INIZZIALIZZAZZIONE VARIABILI ============== */ struct tm *timeinfo = NULL; time_t rawtime = 0; time_t ver = 0; char *buffer = NULL; char *nullo = "Orario non disponibile\n"; /* ====================================================================== */ /* ===== ALLOCO LA MEMORIA PER LA STRINGA CHE CONTERRA' ORA E DATA ====== */ buffer = (char *)calloc( 25 , sizeof( char ) ); if( buffer == NULL ) { /* */ fprintf( stderr , "%s" , "memoria per buffer non allocata\n" ); return nullo; } /* ====================================================================== */ /* ====================================================================== */ ver = time( &rawtime ); if( ver == -1 ) { /* */ fprintf( stderr , "%s" , "time() non riuscita\n" ); return nullo; } /* ====================================================================== */ /* ====================================================================== */ timeinfo = localtime( &rawtime ); if( timeinfo == NULL ) { /* */ fprintf( stderr , "%s" , "struct timeinfo non riempita \n" ); return nullo; } /* ====================================================================== */ /* ====================================================================== */ ver = strftime( buffer , 25 , "%d-%m-%Y alle %H:%M:%S" , timeinfo ); if( ver == 0 ) { /* */ fprintf( stderr , "%s" , "strftime non riuscita\n" ); return nullo; } /* ====================================================================== */ return buffer; }
So che esiste la funzione assert() ma non so come utilizzarla per andare a prendere il singolo errore.

Rispondi quotando