Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Programma crasha se aggiungo più di un elemento al DB
Forum - C/C++ - Programma crasha se aggiungo più di un elemento al DB

Avatar
stichtom (Member)
Newbie


Messaggi: 4
Iscritto: 21/10/2012

Segnala al moderatore
Postato alle 17:44
Venerdì, 21/11/2014
Ciao a tutti, sto facendo un esercizio per la gestione di un registro scolastico. L'ho finito e all'inizio sembrava funzionare correttamente. Poi mi sono accorto che se aggiungo più di uno studente al database, il programma crasha. In particolare ho notato che il problema è collegato in qualche modo al salvataggio del nome (forse qualcosa con la realloc), infatti se lavoro solo con i numeri delle matricole non c'è nessun problema.

Qualcuno potrebbe darmi una mano? Il codice completo (in particolare le funzioni add e print sono quelle interessate):

Codice sorgente - presumibilmente C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. #ifdef _WIN32
  6. #define CLEAR "cls"
  7. #else // Negli altri OS
  8. #define CLEAR "clear"
  9. #endif
  10.  
  11. #define CLEAR_BUFFER do { c = getchar(); } while (c != '\n' && c != EOF); // Macro per pulire il buffer in input
  12. #define MAX 100 // Definisco il numero massimo di caratteri per il nome, cognome e indirizzo
  13. #define TRUE 1
  14. #define FALSE 0
  15.  
  16. typedef struct {
  17.     int day;
  18.     int month;
  19.     int year;
  20. } DATE_T;
  21.  
  22. typedef struct {
  23.     int code;
  24.     char *name;
  25.     char *surname;
  26.     char *address;
  27.     DATE_T date;
  28.     int *exams;
  29. } STUDENT_T;
  30.  
  31. STUDENT_T *student;
  32. char courses[][15] = {"Programmazione", "Inglese", "Analisi"}; // SISTEMARE POI
  33. int myIndex = 0; // Contatore che conta il numero degli studenti
  34.  
  35. int checkValue(int value, int check, int min, int max);
  36. void checkAllocation(STUDENT_T *student);
  37. void menu();
  38. void add(STUDENT_T *student);
  39. void printDatabase(STUDENT_T *student);
  40. char* readString();
  41. void exitmenu();
  42.  
  43.  
  44. int main()
  45. {
  46.  
  47.     student = (STUDENT_T*) malloc(sizeof(STUDENT_T));
  48.     checkAllocation(student);
  49.  
  50.     menu();
  51.  
  52.     free(student);
  53.  
  54.     return 0;
  55. }
  56.  
  57. void menu()
  58. {
  59.     int menu, check;
  60.  
  61.     system(CLEAR);
  62.  
  63.     printf("\t\t ---- MENU GESTIONE SEGRETERIA ---- \n\n");
  64.     printf("1 - Inserisci un nuovo studente\n");
  65.     printf("2 - Stampa il registro\n");
  66.     printf("3 - Elimina uno studente\n");
  67.     printf("4 - Ricerca uno studente\n");
  68.     printf("5 - Ricerca studenti per esami da sostenere\n");
  69.     printf("6 - Esci dal programma\n\n");
  70.     printf("Scegli un opzione dal menu: ");
  71.     check = scanf("%d", &menu);
  72.     menu = checkValue(menu, check, 1, 6);
  73.  
  74.     switch (menu) {
  75.     case 1 :
  76.         add(student);
  77.         break;
  78.     case 2 :
  79.         printDatabase(student);
  80.         break;
  81.     case 3 :
  82.         //cancel(student);
  83.         break;
  84.     case 4 :
  85.         //search(student);
  86.         break;
  87.     case 5 :
  88.         //searchSubject(student);
  89.         break;
  90.     case 6 :
  91.         exit(EXIT_SUCCESS);
  92.     default :
  93.         printf("Errore, scelta non valida");
  94.         exit(EXIT_FAILURE);
  95.     }
  96.  
  97.     return;
  98. }
  99.  
  100.  
  101. int checkValue(int value, int check, int min, int max)
  102. {
  103.     int c;
  104.  
  105.     CLEAR_BUFFER
  106.     while (value < min || value > max || check != 1) {
  107.         printf("Errore, devi inserire un valore valido compreso tra %d e %d: ", min, max);
  108.         check = scanf("%d", &value);
  109.         CLEAR_BUFFER
  110.     }
  111.  
  112.     return value;
  113. }
  114.  
  115. void checkAllocation(STUDENT_T *student)
  116. {
  117.     if (student == NULL) {
  118.         printf("Errore durante l'allocazione dinamica della memoria");
  119.         exit(EXIT_FAILURE);
  120.     }
  121.  
  122.     return;
  123. }
  124.  
  125. void exitmenu()
  126. {
  127.     int option, check;
  128.  
  129.     printf("\n\nPremi 0 per tornare al main o 1 per uscire: ");
  130.     check = scanf("%d", &option);
  131.     option = checkValue(option, check, 0, 1);
  132.  
  133.     if (option == 0)
  134.         menu();
  135.     else if (option == 1)
  136.         exit(EXIT_SUCCESS);
  137. }
  138.  
  139. void add(STUDENT_T *student)
  140. {
  141.  
  142.     char c;
  143.     int check, j;
  144.     int i = myIndex;
  145.     /*enum subjects {Programmazione, Inglese, Analisi, Algoritmi, Architettura_Elaboratori, Algebra};
  146.     enum subjects courses;*/
  147.     system(CLEAR);
  148.  
  149.     student = (STUDENT_T*) realloc(student, (myIndex + 1) * sizeof(STUDENT_T));
  150.     checkAllocation(student);
  151.  
  152.     printf("I: %d, INDEX = %d", i, myIndex);
  153.     printf("\t\t ---- MENU GESTIONE SEGRETERIA ---- \n\n");
  154.     printf("Inserisci il numero di matricola: ");
  155.     check = scanf("%d", &student[i].code);
  156.     student[i].code = checkValue(student[i].code, check, 0, INT_MAX);
  157.     for (j = 0; j < myIndex; j++)
  158.         if (student[i].code == student[j].code) {
  159.             printf("Il numero di matricola inserito e\' gia presente nel database");
  160.             exitmenu();
  161.         }
  162.  
  163.     printf("Inserisci il nome: ");
  164.     for (j = 0; j < MAX && (c = getchar()) != '\n'; j++) {
  165.         student[i].name = (char*)realloc(student[i].name , (j+1) * sizeof(char));
  166.         if (student[i].name  == NULL) {
  167.             printf("Errore durante l'allocazione dinamica della memoria");
  168.             exit(EXIT_FAILURE);
  169.         }
  170.         student[i].name[j] = c;
  171.     }
  172.     student[i].name[j] = '\0';
  173.  
  174.     // altri input
  175.  
  176.     myIndex++;
  177.  
  178.     exitmenu();
  179.  
  180.     return;
  181.  
  182. }
  183.  
  184. void printDatabase(STUDENT_T *student)
  185. {
  186.     int i, j;
  187.  
  188.     system(CLEAR);
  189.  
  190.     printf("\t\t ---- VISUALIZZA CONTENUTO DATABASE ---- \n\n");
  191.     printf("INDEX: %d\n\n", myIndex);
  192.     if (myIndex == 0)
  193.         printf("Non ci sono elementi da visualizzare nel database");
  194.     else {
  195.         for (i = 0; i < myIndex; i++) {
  196.             printf("INDEX: %d, i: %d\n\n", myIndex, i);
  197.             printf("Numero matricola: %d\n", student[i].code);
  198.             printf("\tNome e Cognome: %s\n", student[i].name/*, student[i].surname*/);
  199.            // Altre stampe
  200.  
  201.         }
  202.  
  203.     }
  204.  
  205.     exitmenu();
  206.  
  207.     return;
  208.  
  209. }



Ultima modifica effettuata da stichtom il 22/11/2014 alle 2:21
PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6403
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 8:19
Sabato, 22/11/2014
Se intendi modificare il puntatore con la realloc, devi usare un puntatore doppio (ovvero passare il puntatore per puntatore)


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
PM Quote