(それはあなたの情報のためのスーパーマーケットのトラフィックをシミュレート)などoperations.Hereが私のコードで、追加、削除、削除やって:リンクリスト
#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);
}
私はこのコードを私のプログラムはaborted.here取得実行しようとする正確な出力です:
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
この出来事で、どのように私はそれを修正することができますなぜあなたは私に言うことはできますか?ありがとうございます。私のコードで修正する必要がある他の問題はありますか?
'malloc()'関数にエンコードされたアサーションは失敗しました。その関数が意図したとおりのアサーションを使用していると仮定すると、その関数が依存している(そしてエラーメッセージのテキストで与えられている)想定される不変量は、実行時には満たされないことがわかります。これは、あなたのコードが、動的に割り当てられた記憶域の境界を越え、ポインタが一度指摘されたブロックが解放された後にポインタを使用したり、ポインタを二重に解放したりしたことが原因です。 –
'item = malloc(sizeof(item));' - > 'item = malloc(sizeof * item);'を開始します。あなたのオブジェクトにあなたの型と同じ名前をつけないようにするのが良い理由、あるいはそれ以上のこと:typedefを全く使わないでください) – wildplasser
私は気付いたことのひとつに 'void create_item(item * fitem)'があります。それは新しいアイテムを構築するようです。しかし、実際にはそのアイテムを返すわけではありません。それはそれを失う。新しい項目を返すために 'fitem'パラメータを使いたいと思っていたら、これはやり方ではなく、うまくいきません。 –