【问题标题】:Using free() for char-pointer in struct使用 free() 作为 struct 中的字符指针
【发布时间】:2020-10-16 23:01:19
【问题描述】:
typedef struct inventory 
{
   char *name; 
   int quantity; 
   double price; 
   struct inventory* next_inventory; 
   
} invent;

int main(void)
{
   invent *one=malloc(sizeof(invent)); 
   invent *two=malloc(sizeof(invent));
   invent *three=malloc(sizeof(invent));

   one->next_inventory=two;
   two->next_inventory=three;
   three->next_inventory=NULL;

   one->name=malloc(256);
   ...(repeat two->name, three->name)
   
   printf("name: ");
   scanf("%s", one->name);

   printf("qunatity: ");
   scanf("%d", &one->quantity);

   printf("price: ");
   scanf("%lf", &one->price);
   
   ...(repeat scanf() for two, three)

   while(one!=NULL)
   {
      printf("%s %d %.0f\n", one->name, one->quantity, one->price);
      printf("check\n");
      one=one->next_inventory;
   }

   free(one->name);
   free(two->name);
   free(three->name);
   free(one);
   free(two);
   free(three);

   return 0;

free(one->name);不起作用。我检查了另一个 free(),使用 printf("check");, two->name, three->name, one ...它起作用了。为什么只有 free(one->name) 不起作用?我能做些什么来解决这个问题?给我建议。

【问题讨论】:

  • free(one->name); doesn't work“不起作用”是什么意思?您如何确切地看到该陈述“不起作用”?
  • 那么one 是NULL。我知道它是 NULL,因为如果它不是 NULL,那么循环会一直循环。
  • @KamilCuk 分段错误:11。程序已关闭。
  • @user253751 那我该怎么解决这个问题呢?我应该返回我使用的内存。(使用 malloc)
  • 如果库存中有 100 个元素怎么办?你继续做一个->next_inventory->next_inventory...100 次...->name ?

标签: c struct malloc char-pointer


【解决方案1】:

您正在使用实际的 'one' 变量进行迭代,最后您尝试 free(one) 其中 one 指向 NULL。

您不应该忘记您请求分配的内存地址,以便以后能够使用free 释放该内存。

我会使用一个临时指针来迭代:

 invent* tmp = one;
 while(tmp!=NULL)
   {
      printf("%s %d %.0f\n", tmp->name, tmp->quantity, tmp->price);
      printf("check\n");
      tmp=tmp->next_inventory;
   }

// free all names and one, two, three.

替代方案:

你可以定义一个打印函数:

void printAll(invent* one)
{
    while(one!=NULL)
   {
      printf("%s %d %.0f\n", one->name, one->quantity, one->price);
      one=one->next_inventory;
   }
}

然后在main中调用

printAll(one)

这是因为指针在函数中按值传递,您不会丢失原始地址。

【讨论】:

  • 这是我的功课,所以有条件。 struct 必须有 char *name,没有 struct 1、2、3 的不要做 struct,只用 struct 1 打印。感谢您的建议,但我不能使用临时指针。
  • 您不能使用one 进行迭代,因为在您完成打印后如何知道one 最初指向的位置?
  • 我的教授说“只有one, two, three pointer指向struct。所以我不能使用temporary pointer。我该怎么办?
【解决方案2】:

这是您的程序的改进版本,其中还包括检查 malloc 返回码:

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

typedef struct invent 
{
   char *name; 
   int quantity; 
   double price; 
   struct invent* next_inventory; 
   
} invent;

void enter(invent **s)
{
   printf("name: ");
   scanf("%s", (*s)->name);

   printf("quantity: ");
   scanf("%d", &(*s)->quantity);

   printf("price: ");
   scanf("%lf", &(*s)->price);
   
}

void *alloc(size_t size)
{
    void *p;
    
    p = malloc(size);
    if (p == NULL)
    {
        perror("malloc");
        exit(1);
    }
    return p;
}

int main(void)
{
   invent *start;

   invent *one;
   invent *two;
   invent  *three;

   one = alloc(sizeof(invent)); 
   two = alloc(sizeof(invent));
   three = alloc(sizeof(invent));

   start = one;

   one->next_inventory=two;
   two->next_inventory=three;
   three->next_inventory=NULL;

   one->name=alloc(256);
   two->name=alloc(256);
   three->name=alloc(256);


   enter(&one);
   enter(&two);
   enter(&three);

   while(one != NULL)
   {
      printf("%s %d %.0f\n", one->name, one->quantity, one->price);
      printf("check\n");
      one=one->next_inventory;
   }

   free(start->name);
   free(two->name);
   free(three->name);
   free(start);
   free(two);
   free(three);

   return 0;

}

【讨论】:

  • 感谢您的建议,pifor。但是我的教授说“只有one, two, three pointer指向struct。不使用start我该怎么办?
  • 请给出你所拥有的所有规范:你真的需要一个循环来打印数据吗?你真的需要释放内存吗? C结构是强制性的吗?我们可以修改这个结构吗?等等。这个作业看起来有点太做作了……
  • 我解决了问题。谢谢pifor!!!想来想去,打印数据不需要循环。
猜你喜欢
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 2021-11-27
相关资源
最近更新 更多