0
点赞
收藏
分享

微信扫一扫

c语言实现双向链表在Linux系统gcc4.8版本下报错

llist.h文件

#ifndef LLIST_H__
#define LLIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MODE_HEAD 0
#define MODE_TAIL 1

struct node
{
struct node *prev;
struct node *next;
char data[]; //使用柔性数组占位置,缺点是标准c99才引入,兼容所版本可以定义为:char data[1];
};

typedef struct head
{
int size;
struct node ele;
}LList;

LList *creat(int initszie);

LList *insert(int mode, const void *data, LList *pList);

void travel(LList *pList, void (*print)(const void *data));

int destory(LList *pList);

#endif

llist.c文件

#include "llist.h"

LList *creat(int initszie)
{
LList *pList = (LList *)malloc(sizeof(struct head));
if(pList == NULL)
{
return NULL;
}
pList->size = initszie;
pList->ele.prev = &pList->ele;
pList->ele.next = &pList->ele;
return pList;
}

LList *insert(int mode, const void *data, LList *pList)
{
struct node *newnode = (struct node *)malloc((sizeof(struct node *) + pList->size));
if(newnode == NULL)
{
return NULL;
}
memcpy(newnode->data, data, pList->size);
if(mode == MODE_HEAD)
{
newnode->prev = &pList->ele;
newnode->next = pList->ele.next;
}
else if(mode == MODE_TAIL)
{
newnode->prev = pList->ele.prev;
newnode->next = &pList->ele;
}
else
{
return NULL;
}
newnode->prev->next = newnode;
newnode->next->prev = newnode;
return pList;
}

void travel(LList *pList, void (*print)(const void *data))
{
for(struct node *cur = pList->ele.next; cur != &pList->ele; cur = cur->next)
{
print(cur->data);
}
return;
}

int destory(LList *pList)
{
struct node *cur, *node = NULL;
for(cur = pList->ele.next; cur != &pList->ele; cur = node)
{
node = cur->next;
free(cur);
cur = NULL;
}
free(pList);
pList = NULL;
return 0;
}

main.c文件

#include "llist.h"

typedef struct Element
{
char name[32];
int age;
double score;
}ElementType;

void print(const void *data)
{
const struct Element *e = (struct Element *)data;
printf("data >> [name: %s, age: %d, score: %lf].\n", e->name, e->age, e->score);
return;
}

int main()
{

int data_size = sizeof(ElementType);
LList *pList = creat(data_size);
if(pList == NULL)
{
printf("crete failed.\n");
}
else
{
printf("create success addr is [%p].\n", pList);
}

printf("create----------------------->pList addr is [%p].\n", pList);

struct Element arr[] = {{"one", 1, 11.1}, {"two", 2, 12.2}, {"three", 3, 13.3}, {"four", 4, 14.4}, {"five", 5, 15.5}, {"six", 6, 16.6}, {"seven", 7, 17.7}};
for(int i = 0; i < sizeof(arr)/sizeof(ElementType); i++)
{
LList *result = insert(1, &arr[i], pList);
if(result == NULL)
{
printf("[%d] insert failed.\n", i);
}
else
{
printf("[%d] insert success.\n", i);
}
}

printf("insert----------------------->pList addr is [%p].\n", pList);

travel(pList, print);

printf("travel----------------------->pList addr is [%p].\n", pList);

printf("double link list destory status [%d].\n", destory(pList));

return 0;
}

报错如下:

szxphisprb02499:/run/app/list/double/lib2 # gcc llist.c  main.c -std=c99 -o main
szxphisprb02499:/run/app/list/double/lib2 # ./main
create success addr is [0x18f8010].
[0] insert success.
[1] insert success.
[2] insert success.
[3] insert success.
[4] insert success.
[5] insert success.
[6] insert success.
data >> [name: one, age: 1, score: 0.000000].
data >> [name: two, age: 2, score: 0.000000].
data >> [name: three, age: 3, score: 0.000000].
data >> [name: four, age: 4, score: 0.000000].
data >> [name: five, age: 5, score: 0.000000].
data >> [name: six, age: 6, score: 0.000000].
data >> [name: seven, age: 7, score: 17.700000].
*** Error in `./main': free(): invalid next size (fast): 0x00000000018f81b0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7411f)[0x7f87eb75811f]
/lib64/libc.so.6(+0x79596)[0x7f87eb75d596]
/lib64/libc.so.6(+0x7a3db)[0x7f87eb75e3db]
./main[0x40086e]
./main[0x4009e7]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f87eb704725]
./main[0x4005f9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:15 847106934 /run/app/list/double/lib2/main
00601000-00602000 r--p 00001000 00:15 847106934 /run/app/list/double/lib2/main
00602000-00603000 rw-p 00002000 00:15 847106934 /run/app/list/double/lib2/main
018f8000-01919000 rw-p 00000000 00:00 0 [heap]
7f87e4000000-7f87e4021000 rw-p 00000000 00:00 0
7f87e4021000-7f87e8000000 ---p 00000000 00:00 0
7f87eaea3000-7f87eaeba000 r-xp 00000000 ca:02 1362592 /lib64/libgcc_s.so.1
7f87eaeba000-7f87eb0b9000 ---p 00017000 ca:02 1362592 /lib64/libgcc_s.so.1
7f87eb0b9000-7f87eb0ba000 r--p 00016000 ca:02 1362592 /lib64/libgcc_s.so.1
7f87eb0ba000-7f87eb0bb000 rw-p 00017000 ca:02 1362592 /lib64/libgcc_s.so.1
7f87eb0bb000-7f87eb0c2000 r-xp 00000000 ca:02 1362572 /lib64/librt-2.22.so
7f87eb0c2000-7f87eb2c1000 ---p 00007000 ca:02 1362572 /lib64/librt-2.22.so
7f87eb2c1000-7f87eb2c2000 r--p 00006000 ca:02 1362572 /lib64/librt-2.22.so
7f87eb2c2000-7f87eb2c3000 rw-p 00007000 ca:02 1362572 /lib64/librt-2.22.so
7f87eb2c3000-7f87eb2db000 r-xp 00000000 ca:02 1362568 /lib64/libpthread-2.22.so
7f87eb2db000-7f87eb4da000 ---p 00018000 ca:02 1362568 /lib64/libpthread-2.22.so
7f87eb4da000-7f87eb4db000 r--p 00017000 ca:02 1362568 /lib64/libpthread-2.22.so
7f87eb4db000-7f87eb4dc000 rw-p 00018000 ca:02 1362568 /lib64/libpthread-2.22.so
7f87eb4dc000-7f87eb4e0000 rw-p 00000000 00:00 0
7f87eb4e0000-7f87eb4e2000 r-xp 00000000 ca:02 1362544 /lib64/libdl-2.22.so
7f87eb4e2000-7f87eb6e2000 ---p 00002000 ca:02 1362544 /lib64/libdl-2.22.so
7f87eb6e2000-7f87eb6e3000 r--p 00002000 ca:02 1362544 /lib64/libdl-2.22.so
7f87eb6e3000-7f87eb6e4000 rw-p 00003000 ca:02 1362544 /lib64/libdl-2.22.so
7f87eb6e4000-7f87eb87f000 r-xp 00000000 ca:02 1362538 /lib64/libc-2.22.so
7f87eb87f000-7f87eba7f000 ---p 0019b000 ca:02 1362538 /lib64/libc-2.22.so
7f87eba7f000-7f87eba83000 r--p 0019b000 ca:02 1362538 /lib64/libc-2.22.so
7f87eba83000-7f87eba85000 rw-p 0019f000 ca:02 1362538 /lib64/libc-2.22.so
7f87eba85000-7f87eba89000 rw-p 00000000 00:00 0
7f87eba89000-7f87eba8e000 r-xp 00000000 ca:02 1362760 /lib64/libachk.so
7f87eba8e000-7f87ebc8d000 ---p 00005000 ca:02 1362760 /lib64/libachk.so
7f87ebc8d000-7f87ebc8e000 r--p 00004000 ca:02 1362760 /lib64/libachk.so
7f87ebc8e000-7f87ebc8f000 rw-p 00005000 ca:02 1362760 /lib64/libachk.so
7f87ebc8f000-7f87ebcb0000 r-xp 00000000 ca:02 1362530 /lib64/ld-2.22.so
7f87ebe94000-7f87ebe98000 rw-p 00000000 00:00 0
7f87ebead000-7f87ebeb0000 rw-p 00000000 00:00 0
7f87ebeb0000-7f87ebeb1000 r--p 00021000 ca:02 1362530 /lib64/ld-2.22.so
7f87ebeb1000-7f87ebeb2000 rw-p 00022000 ca:02 1362530 /lib64/ld-2.22.so
7f87ebeb2000-7f87ebeb3000 rw-p 00000000 00:00 0
7ffea0286000-7ffea02a7000 rw-p 00000000 00:00 0 [stack]
7ffea02e1000-7ffea02e4000 r--p 00000000 00:00 0 [vvar]
7ffea02e4000-7ffea02e6000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)

初步定位到是llist.c文件中destory方法中循环删除节点的释放代码问题,但不知什么原因,在自己的mac电脑上跑这段代码是没问题,求原因?

c语言实现双向链表在Linux系统gcc4.8版本下报错_#include

举报

相关推荐

0 条评论