C链表程序不会显示字符串

时间:2019-04-28 21:36:27

标签: c linked-list buffer

我在处理链表的作业中遇到问题。 问题是,当我尝试打印它们时,并不是所有数据都显示出来。 这是我的输入:

insert JablkaJonagold AppleVillage 10 0.35 kg
display

这是我的输出:

Name=, Manufacturer=AppleVillage, Quantity=10.000000, Price=0.350000, MJ=kg

但是我的输出应如下所示:

Name=JablkaJonagold, Manufacturer=AppleVillage, Quantity=10.000000, Price=0.350000, MJ=kg

我尝试了许多编译器,并且运行正常,希望在rextester.com上出现错误,

*** buffer overflow detected ***: /var/www/service/usercode/973543283/a.out terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7ff45289b7e5]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x5c)[0x7ff45293c56c]
/lib/x86_64-linux-gnu/libc.so.6(+0x116570)[0x7ff45293a570]
/lib/x86_64-linux-gnu/libc.so.6(+0x1158c2)[0x7ff4529398c2]
/var/www/service/usercode/973543283/a.out[0x4008d9]
/var/www/service/usercode/973543283/a.out[0x4006b9]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7ff452844830]
/var/www/service/usercode/973543283/a.out[0x400739]
======= Memory map: ========
00400000-00401000 r-xp 00000000 b6:4f3a1 2919345                         /var/www/service/usercode/973543283/a.out
00600000-00601000 r--p 00000000 b6:4f3a1 2919345                         /var/www/service/usercode/973543283/a.out
00601000-00602000 rw-p 00001000 b6:4f3a1 2919345                         /var/www/service/usercode/973543283/a.out
024c1000-024e2000 rw-p 00000000 00:00 0                                  [heap]
7ff45260e000-7ff452624000 r-xp 00000000 b6:4f3a1 105                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7ff452624000-7ff452823000 ---p 00016000 b6:4f3a1 105                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7ff452823000-7ff452824000 rw-p 00015000 b6:4f3a1 105                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7ff452824000-7ff4529e3000 r-xp 00000000 b6:4f3a1 10849                   /lib/x86_64-linux-gnu/libc-2.23.so
7ff4529e3000-7ff452be3000 ---p 001bf000 b6:4f3a1 10849                   /lib/x86_64-linux-gnu/libc-2.23.so
7ff452be3000-7ff452be7000 r--p 001bf000 b6:4f3a1 10849                   /lib/x86_64-linux-gnu/libc-2.23.so
7ff452be7000-7ff452be9000 rw-p 001c3000 b6:4f3a1 10849                   /lib/x86_64-linux-gnu/libc-2.23.so
7ff452be9000-7ff452bed000 rw-p 00000000 00:00 0 
7ff452bed000-7ff452c13000 r-xp 00000000 b6:4f3a1 10838                   /lib/x86_64-linux-gnu/ld-2.23.so
7ff452de1000-7ff452de4000 rw-p 00000000 00:00 0 
7ff452e0f000-7ff452e12000 rw-p 00000000 00:00 0 
7ff452e12000-7ff452e13000 r--p 00025000 b6:4f3a1 10838                   /lib/x86_64-linux-gnu/ld-2.23.so
7ff452e13000-7ff452e14000 rw-p 00026000 b6:4f3a1 10838                   /lib/x86_64-linux-gnu/ld-2.23.so
7ff452e14000-7ff452e15000 rw-p 00000000 00:00 0 
7ffee1fbb000-7ffee1fd0000 rw-p 00000000 00:00 0                          [stack]
7ffee1ff9000-7ffee1ffb000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

Abort signal from abort(3) (SIGABRT)



这是我的代码:

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

struct merchandise
{
  char name[50];
  char manufacturer[50];
  double quantity;
  double price;
  char unit[2];
  struct merchandise *next;
};

struct shop
{
  struct merchandise *first;
};

struct shop *create_head ()
{
  struct shop *head = (struct shop *)malloc(sizeof(struct shop));
  head -> first = NULL;
  return head;
}

void insert (struct shop *head)
{
  struct merchandise *temp = (struct merchandise *) malloc (sizeof(struct merchandise));
  temp -> next = NULL;


  char name[50];
  char manufacturer[50];
  double quantity;
  double price;
  char unit[2];

  scanf("%s %s %lf %lf %s\n", name, manufacturer, &quantity, &price, unit);

  if (head -> first == NULL)
    head -> first = temp;

  else
  {
    struct merchandise *temp2 = head -> first;
    while (temp2 -> next != NULL)
      temp2 = temp2 -> next;

    temp2 -> next = temp;
    temp -> next = NULL;
  }

  strcpy(temp -> name, name);
  strcpy(temp -> manufacturer, manufacturer);
  temp -> quantity = quantity;
  temp -> price = price;
  strcpy(temp -> unit, unit);
}

void display (struct shop *head)
{
  struct merchandise *temp = head -> first;
  while (temp != NULL)
  {
    printf("Name=%s, Manufacturer=%s, Quantity=%lf, Price=%lf, MJ=%s\n", temp -> name, temp -> manufacturer, temp -> quantity, temp -> price, temp -> unit);
    temp = temp -> next;
  }
  printf("\n");
}

int main()
{
  char select[20];
  struct shop *head = create_head();
  while (scanf("%s", select) > 0)
  {
    if (strcmp(select, "insert") == 0)
    {
      if (head->first != NULL)
      {
        free(head);
        struct shop *head = create_head();
      }
      insert (head);
    }

    if (strcmp(select, "display") == 0)
      display (head);
  }


  return 0;
}

0 个答案:

没有答案