尝试学习malloc的问题

时间:2012-11-09 23:24:21

标签: c malloc buffer

当用户输入朋友的信息时,我希望指针被分配适当的空间,并且朋友信息存储在该分配的空间中。我已经阅读了其他提到使用缓冲区数组作为scanf参数的地方的snippits,但我只是把它们放在一起。这是我到目前为止所拥有的。

#include <stdio.h>  
#include <string.h>
#include <stdlib.h>
#include <conio.h>


//Structure for contacts
typedef struct friends_contact{

   char *First_Name;
   char *Last_Name;
   char *home;
   char *cell;
 }fr;

void menu(fr*friends ,int* counter,int user_entry,int i);
void setFirst(fr*,int *,int i);
char getFirst(fr*,int i);
void add_contact(fr*friends,int* counter,int i);
void print_contact(fr*friends ,int* counter, int i);

int main() 
{

  int user_entry=0;
  fr friends[5];
  int buffer[50];
  int counter=0;
  int i=0;
   for(i=0;i<5;i++)
    {
     friends[i].First_Name = (char*) malloc(sizeof(char) * 64); 
     free(friends[i].First_Name);
    }    
  menu(friends, &counter,user_entry,i);
  getch();
  return 0;
}
 //Menu function
 void menu(fr*friends,int* counter,int user_entry, int i) 
{

   do{
      int result;

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

         if(user_entry==1)
            {
            add_contact(friends,counter,i);
            }
            if(user_entry==2)
            {

            } 
            if(user_entry==3)
            {


            }                  
            if(user_entry==4)
            {
            print_contact(friends, counter,i);
            } 
       }while(user_entry!=5);                 
}

void setFirst(fr*friends, int* counter, int i) 
{
    //malloc issue **
    friends=(fr*) malloc(sizeof(fr));

    printf("Enter a first name \n");
    scanf("%s",friends[*counter].First_Name);



}
char getFirst(fr*friends , int pos) 
{

    printf("%s ", friends[pos].First_Name);
    return *friends[pos].First_Name;
}
void add_contact(fr*friends,int* counter,int i) 
{

    setFirst(friends,counter,i); 
    (*counter)++;
}
void print_contact(fr*friends ,int* counter, int i) 
{

  for( i = 0; i < *counter; i++)
    if (strlen(friends[i].First_Name))
    {
        getFirst(friends, i);
    }
 }

这显然只是代码的一部分,现在我在添加名称函数中输入名称后出现了分段错误。它在退出之前最后一次循环到菜单。我意识到我在某个地方出了问题,我想尝试用缓冲解决方案解决这个问题。解决任何人?

1 个答案:

答案 0 :(得分:0)

您的fr变量是指向已经分配了空间的堆栈上某个位置的指针。你不应该使用它 - 它已经初始化,你不能malloc它(或至少你永远不应该)。你应该传递一个指针并初始化它。

也就是说:

fr *friends;

然后当你想分配空间时

friends = (fr*) malloc(sizeof(fr)* NUMBER OF FR YOU WANT);

此外,如果要使用它们,则需要初始化结构的元素,因为它们都是指针。这将类似地完成

friends[i].First_Name = (char*) malloc(sizeof(char)* (LENGTH OF STRING + 1));

或者它是friends[i]->First_Name,具体取决于您将其发送到您的功能的方式。

一个好的解决方案就是初始化main

中的结构成员
int i;
fr friends[5];
for(i=0;i<5;i++)
{
   friends[i].First_Name = (char*) malloc(sizeof(char) * 64);
   friends[i].Last_Name = (char*) malloc(sizeof(char) * 64);
   friends[i].home = (char*) malloc(sizeof(char) * 64);
   friends[i].cell = (char*) malloc(sizeof(char) * 64);
}
//
//Do functions (without malloc'ing)
//
for(i=0;i<5;i++)
{
   free(friends[i].First_Name);
   free(friends[i].Last_Name);
   free(friends[i].home);
   free(friends[i].cell);
}

这里我选择了64作为字符串的长度。您可以将其设置为您认为有效的任何内容。

底线是您必须先为指针分配空间才能使用它。否则它不指向你可以使用的任何内存,你会得到一个分段错误。此外,一旦完成了已分配的内存,不要忘记释放。请记住,一旦你释放它就不能使用它(除非你再次使用它)。

相关问题