c - 链表内的链表

标签 c linked-list

我想实现一个在链表中使用链表的程序(它模拟超市的流量以获取您的信息)执行添加、删除、删除等操作。这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct cash_queue cash;
typedef struct client client;
typedef struct item item;
int k,c;//k== cash registers,c == customers
struct item{
  int value;
  struct item* next;
};

struct cash_queue{
   int cash_num;
   struct client *first;
   struct cash_que* next;
};  

struct client{
  int client_num;
  int items;
  struct item *fitem;
  struct client* next;
};
void create_item(item* fitem){ 
  item *item,*cur;
  cur=fitem;
  item=malloc(sizeof(item));
  printf("give product value\n");
  int v;
  scanf(" %d",&v);
  item->value=v;
  printf("value: %d\n",item->value);
  item->next=NULL;
  while (cur->next)
   cur=cur->next;
  cur->next=item;
} 

void create_queue(client* first){
  client *client,*cur;
  cur=first;    
  client=malloc(sizeof(client));
  printf("how many items this client has?\n");
  int x,i;
  scanf("%d",&x);
  client->items=x;
  client->next=NULL;
  client->fitem=malloc(sizeof(item));
  for (i=1;i<=x;++i)
    create_item(client->fitem);
  while (cur->next){
    cur=cur->next;
  }
  cur->next=client;
}
int main(){
   cash* ncash;
  ncash=malloc(sizeof(cash));
  ncash->cash_num=1;
  ncash->next=NULL;
  ncash->first=malloc(sizeof(client));
      printf("give the number of starting customers\n");
  scanf("%d",&c);
  int i;
  for(i=1;i<=c;++i)
    create_queue(ncash->first);
  }

当我尝试执行此代码时,我的程序被中止。这是确切的输出:

 give the number of starting customers
 3
 how many items this client has?
 1
 give product value
  2
 value: 2
 aa: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char              *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size        & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
  Aborted

您能告诉我为什么会发生这种情况以及如何解决它吗?谢谢。我的代码中还有其他问题需要纠正吗?

最佳答案

正如我在评论中所写,错误消息可能意味着您以某种方式滥用动态分配的内存。看看你的代码,确实是这样。您有此模式的多个实例:

typedef struct foo foo;

struct foo {
    // elements ...
};

void f() {
    foo *foo;
    foo = malloc(sizeof(foo));
    // ... populate foo ..
}

你试图变得太聪明,从而使自己或编译器或两者都感到困惑。包含 malloc() 调用的语句中的两个 foo 是同一件事:变量 foo,其中是 struct foo * 类型的指针。除非 struct foo 的大小恰好与系统上指针的大小相同,否则这是错误的做法。如果结构体大于指针,则写入其某些成员确实会超出分配的边界。

有多种方法可以解决这个问题。我当然会建议你在命名上更有特色一点。我建议您也取消 typedef——您的类型并没有那么复杂,您可以通过它们获得很多好处,并且它会降低您的代码清晰度。此外,我建议您的所有 malloc() 调用使用此表单:

bar = malloc(n * sizeof(*bar));

请注意,分配的字节数是目标指针指向的对象类型大小的倍数,这一点很明显,但它并不直接取决于 bar 的类型实际上是。总的来说,那么:

struct bar {
    // elements ...
};

void f() {
    struct bar *temp_bar;
    temp_bar = malloc(sizeof(*temp_bar));
    // ... populate temp_bar ..
}

关于c - 链表内的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37034714/

相关文章:

android - 使用 gcc 的 C++ 前端编译 C 代码的好处

创建新的嵌套结构列表

c - 函数执行后指针为空

由于字节顺序错误,无法使用 tcp.h 编译简单程序?

c - "return ret < 0"是什么意思?

c - SOL_SOCKET 有什么用?

java - 将项目添加到列表后打印链表的内容

java - 将文件内容复制到链表数组中并对其进行排序

java - 给定由 LinkedList 组成的 java hashmap 中的键,如何更新值?

c - 如何在 C 中搜索命令行参数?