2

我有一个电话簿应用程序,过去几天我一直在尝试将 malloc 添加到其中,但是由于我是 C 新手,而且我所拥有的书没有详细介绍我想要的内容,所以我不确定所有的技巧。是否可以在用户输入信息时使用 malloc,然后free()在用户选择从电话簿中删除个人时用作删除用户输入的方法?现在无论我是否使用free(),在用户删除条目然后尝试检查电话簿后,程序都会崩溃。我假设(但我们知道这意味着什么)它与不正确地释放或不释放内存有关。这是代码。

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define BUFFER 50
//Structure for contacts
typedef struct friends_contact{

   char *First_Name;
   char *Last_Name;
   char *home;
   char *cell;
 }fr;
 //Function declarations 
 void menu(fr*friends ,int* counter,int user_entry,int i,char newbuddy[]);
 void setFirst(fr*,int *,int i,char newbuddy[]);
 char getFirst(fr*,int i);
 void setLast(fr*friends, int* counter, int i,char newbuddy[]);
 char getLast(fr*friends , int i);
 void setHome(fr*friends, int* counter, int i,char newbuddy[]);
 char getHome(fr*friends, int i);
 void setCell(fr*friends, int* counter, int i,char newbuddy[]);
 char getCell(fr*friends, int i);
 void add_contact(fr*friends,int* counter,int i,char newbuddy[]);
 void print_contact(fr*friends ,int* counter, int i);
 char delete_contact(fr*friends ,int* counter, int i);
 char show_contact(fr*friends ,int* counter, int i);

 int main() {

   int user_entry=0;
   fr friends[5];
   char newbuddy[BUFFER];
   int counter=0;
   int i=0;

   menu(friends, &counter,user_entry,i,newbuddy);

   getch();
   return 0;
  }
  //Menu function
  void menu(fr*friends,int* counter,int user_entry, int i,char newbuddy[]) {

  do{
     int result;

     printf("\nPhone Book Application\n");
     printf("1) Add friend\n2) Delete friend\n3) Show a friend\n4) Show phonebook\n5)Exit\n");   
     scanf("%d", &user_entry);

     if(user_entry==1)
       {
       add_contact(friends,counter,i,newbuddy);
       }
     if(user_entry==2)
       {
       delete_contact(friends ,counter,i);
       } 
     if(user_entry==3)
       {
       result=show_contact(friends ,counter,i);
           if(result==0){
                  printf("\nName not Found\n");
                  }else{
                        result;
                        }

       }                  
    if(user_entry==4)
      {
      print_contact(friends, counter,i);
      } 
   }while(user_entry!=5);                 
  }
 //Start of Set functions. Each entry has its own set function that gathers the data
void setFirst(fr*friends, int* counter, int i,char newbuddy[]) {


   printf("Enter a first name \n");

   scanf("%s",newbuddy);

   friends[*counter].First_Name=malloc(BUFFER*strlen(newbuddy));

   strcpy(friends[*counter].First_Name, newbuddy);

}

void setLast(fr*friends, int* counter, int i,char newbuddy[]) {

   printf("Enter a last name \n");
   scanf("%s",newbuddy);

   friends[*counter].Last_Name=malloc(BUFFER*strlen(newbuddy));

   strcpy(friends[*counter].Last_Name, newbuddy);
}
void setHome(fr*friends, int* counter, int i,char newbuddy[]) {

   printf("Enter a home number \n");
   scanf("%s",newbuddy);

   friends[*counter].home=malloc(BUFFER*strlen(newbuddy));

   strcpy(friends[*counter].home, newbuddy);
}
void setCell(fr*friends, int* counter, int i,char newbuddy[]) {

   printf("Enter a cell number \n");
   scanf("%s",newbuddy);

   friends[*counter].cell=malloc(BUFFER*strlen(newbuddy));

   strcpy(friends[*counter].cell, newbuddy);
}
//Start of Get functions. Each function sends the data to the executing function.
char getFirst(fr*friends , int pos) {

   printf("%s ", friends[pos].First_Name);
   return *friends[pos].First_Name;
 }

 char getLast(fr*friends , int pos) {

   printf("%s\n", friends[pos].Last_Name);
   return *friends[pos].Last_Name;

 }

 char getHome(fr*friends , int pos) {

   printf("(Home) ""%s\n", friends[pos].home);
   return *friends[pos].home;
 }

 char getCell(fr*friends , int pos) {

   printf("(Cell) ""%s\n", friends[pos].cell);
   return *friends[pos].cell;
 }
 //This function allows for the all the set functions to be added.
 void add_contact(fr*friends,int* counter,int i,char newbuddy[]) {




   setFirst(friends,counter,i,newbuddy); 
   setLast(friends,counter,i,newbuddy);
   setHome(friends,counter,i,newbuddy);
   setCell(friends,counter,i,newbuddy);
   (*counter)++;
}

//This is used to delete a name out of the book.
char delete_contact(fr*friends ,int* counter, int i)
{
  char name_search[50]={'\0'};
  char Delete[5]={'\0'};
  printf("Search by last name\n");
  scanf("%s",name_search);//Name entry

  for(i=0;i<*counter;i++)
    {
    if(strcmp(name_search,friends[i].Last_Name)==0)//Copys over the name entered
       {

        strcpy(friends[i].Last_Name,Delete);
        (*counter)++;




        printf("\nName has been deleted\n");
       }
     }  
} 
//This function prints out all the contact information
void print_contact(fr*friends ,int* counter, int i) {

  for( i = 0; i < *counter; i++)
    if (strlen(friends[i].First_Name) && strlen(friends[i].Last_Name)&&   strlen(friends[i].home)&& strlen(friends[i].cell ))
   {
        getFirst(friends, i);
        getLast(friends, i);
        getHome(friends, i);
        getCell(friends, i);
   }
}
//Displays the contact in which you are searching for.
char show_contact(fr*friends ,int* counter, int i) 
{  
    char name_search2[50]={'\0'};
    printf("Please enter a last name\n");
    scanf("%s",name_search2);
    for(i=0;i<*counter;i++)
      {
     //If the name is found, it reaturns the contact info.
        if(strcmp(name_search2,friends[i].Last_Name)==0)
          {

          return printf("%s " "%s" "(Home)""%s""(Cell)" "%s\n",friends[i].First_Name,     friends[i].Last_Name,friends[i].home,friends[i].cell);

           }

        }

  return 0;
}        

我没有添加free()代码,因为不管它似乎得到了相同的结果。为什么只有在我从列表中删除名称后程序才会崩溃?

4

1 回答 1

0

第 1 步:隔离问题:

//This is used to delete a name out of the book.
char delete_contact(fr*friends ,int* counter, int i)
{
  char name_search[50]={'\0'};
  char Delete[5]={'\0'};
  printf("Search by last name\n");
  scanf("%s",name_search);//Name entry

  for(i=0;i<*counter;i++)
    {
    if(strcmp(name_search,friends[i].Last_Name)==0)//Copys over the name entered
       {
        strcpy(friends[i].Last_Name,Delete);
        (*counter)++;
        printf("\nName has been deleted\n");
       }
     }
}

Step2:可能的问题

(*counter)++ //why do this?

当找到要删除的名称时执行此操作。为什么要增加列表中所有名称的计数?我认为这应该是一个递减,或者根本不应该在循环内完成。

于 2012-11-10T18:34:22.213 回答