Question: hello i need help when i quit the program and trying to save and free the memory i get an error about malloc from function
hello i need help when i quit the program and trying to save and free the memory i get an error about malloc from function createAWord()
Please help
Thank You
#define _CRT_SECURE_NO_WARNINGS
#define PAUSE system("pause")
#define FLUSH myFlush()
#define SIZE 5000
#include
#include
#include
#include
typedef struct {
char words[50];
int vowels;
int costants;
}WORDS;
// prototype function
void displayMenu(void);
void displayWords(WORDS* word[],int count);
void freeMem(WORDS* word[],int count);
void myFlush(void);
void input(WORDS* word, int *count);
void load(WORDS* word[], int *count);
void sort(WORDS* word[], int count);
void startingLetter(WORDS* word[], int count);
void save(WORDS* word[], int count);
WORDS* createWord(void);
int userChoice(void);
int main(){
WORDS** word;
int count = 0;
char choice = ' ';
// allocate memory
word = calloc(5000, sizeof(WORDS*));
load(word, &count);
do{
choice = userChoice();
switch(choice){
case 'E':// enter words
word[count] = createWord();
input(word[count], &count);
count++;
break;
case 'P': // Display All Words
displayWords(word, count);
PAUSE;
break;
case'D': // display word with letter selected
startingLetter(word, count);
PAUSE;
break;
case 'Q': // Quit Program
save(word,count);
printf("Thank You ");
freeMem(word,count);
PAUSE;
exit(-1);
break;
default:
printf("Invaild Selection.....Please Try Again ");
PAUSE;
break;
}// end Switch
}while(choice != 'Q');
}// End MAIN
void displayMenu(){
printf("\t************ MENU *************** ");
printf("\t [E]nter a new word ");
printf("\t [P]rint all words ");
printf("\t [D]isplay all words that begin with a certain letter ");
printf("\t [Q]uit ");
printf("\t Enter a choice: ");
}// end displayMenu
void displayWords(WORDS* word[],int count){
int i;
sort(word,count);
printf("Sorted Word in Order: ");
for(i = 0; i < count; i++)
printf("%s\t\t Vowel: %i\t\t Constant: %i ", word[i]->words, word[i]->vowels, word[i]->costants);
}// end displayWords
void freeMem(WORDS* word[],int count){
int i;
for(i = 0; i < count; i++){
printf("%i. %p has been release ", i, word[i]);
free(word);
}// end for
printf("The Array At %p is free ", word);
free(word);
}// end freeMem
void myFlush() {
while (getchar() != ' ');
}//end myFlush
void input(WORDS* word, int *count){
int i;
char letter;
printf("Enter a Word: ");
scanf("%s",word->words);
FLUSH;
word->costants = 0;
word->vowels = 0;
for(i = 0; word->words[i] !='\0';i++){
letter = tolower(word->words[i]);
if(letter == 'a'|| letter == 'e'|| letter == 'i' || letter == 'o' || letter == 'u')
word->vowels++;
else
word->costants++;
}// end for
}// end input
void load(WORDS* word[], int *count){
FILE *ptr;
int i;
ptr = fopen("WCV.bin", "rb"); // open the bin file for reading
if(ptr == NULL){
printf("No Data to load ");
PAUSE;
}else{
fread(count, sizeof(int), 1, ptr);
for(i = 0; i < *count; i++){
word[i] = createWord();
fread(word[i], sizeof(*word[i]), 1, ptr);// read the words
}// end for loop
printf("%i word were load from file ", *count);
fclose(ptr);
}// end else
}// end load
void sort(WORDS* word[], int count) { // sort the word in abc order
int i, j;
WORDS temp;
for (i = 0; i < count; i++)
for (j = i + 1; j < count; j++) {
if (strcmp(word[i]->words, word[j]->words) > 0) {
temp = *word[i];
*word[i] = *word[j];
*word[j] = temp;
}// end if statment
}// end for loop
}// end sort
void startingLetter(WORDS* word[], int count){ // will display word from the letter the user choices
char letter;
int i;
printf("Enter a letter to display word: ");
scanf("%c", &letter);
FLUSH;
sort(word,count);
printf("The word starting with %c ", letter);
for( i = 0; i < count;i++){
if(word[i]->words[0]==letter)
printf("%s\t\tVowels %i\t\t Constant %i ", word[i]->words, word[i]->vowels, word[i]->costants);
}// end for loop
}// end startingLetter
void save(WORDS* word[], int count){
FILE *ptr;
int i;
ptr = fopen("WVC.bin", "wb");
if(ptr == NULL){
printf("Error Saving Data ");
PAUSE;
exit(-1);
}// end if
fwrite(&count, sizeof(*word[i]), 1, ptr);
for(i = 0; i < count; i++)
fwrite(word[i], sizeof(*word[i]), 1, ptr);
fclose(ptr);
}// end save
WORDS* createWord(){
WORDS* result;
result = malloc(sizeof(WORDS));
return result;
}// end createWord
int userChoice(){
char result;
displayMenu();
scanf("%c",&result);
FLUSH;
return toupper(result);
}// end userChoice
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
