c - malloc problem when writing on a file -


a garbage data appears whenever write data in text file... why that? here's code... thanks

int main(void) {    unsigned int option = 0;    int = 0;    }    getch();    while(option != 5){       option = display();        switch(option){           case 5: save();                   break;               }        for(i = 0; < recordctr; i++){          free(array[i]);}      }  }    save(){      file *stream = null;      stream = fopen("student.txt", "wt");      printf("\nsaving student list directory. wait moment please...");              int =0;                  (i=0; i<3; i++){                     fprintf(stream, "%5s %30s %5s\n", array[i]->studentid, array[i]->name, array[i]->course);                  }      fclose(stream);                      } 

there bugs.

  1. recordctr incremented. if chose add 2 times fill array[0], array[1] , array[2]. when freeing memory, freeing recordctr value. in case happen free 6 student records. disaster.
  2. as switch ends freeing student records memory. if user choose exit save() caled tries save student records freed already. bad idea read freed memory.

when see garbage data. mean under input?


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -