| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1452 人关注过本帖
标题:双向链表问题,debug 在while((tmp->pnext) !=NULL) stm32会重启
只看楼主 加入收藏
j15506271841
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2021-8-19
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
双向链表问题,debug 在while((tmp->pnext) !=NULL) stm32会重启

typedef struct {
    uint8_t title_num;
    DRAW_PARA *title;
    uint8_t content_num;
    DRAW_PARA *content;
}PAGE_DATA;

typedef struct page_config{

    uint8_t page_n;
    PAGE_DATA *temp;
    char (*pdata)[40];
    struct page_config *pnext;
    struct page_config *pprev;

}PAGE_CONFIG;


PAGE_CONFIG* initLine(PAGE_CONFIG * head){
  
    head=(PAGE_CONFIG*)malloc(sizeof(PAGE_CONFIG));
 
    head->pprev=NULL;
    head->pnext=NULL;

    PAGE_CONFIG * list=head;
   
    for (int i=0; i<5; i++) {
        
        PAGE_CONFIG * body=(PAGE_CONFIG*)malloc(sizeof(PAGE_CONFIG));
        body->pprev=NULL;
        body->pnext=NULL;
        body->temp = &page_data[i];
        body->page_n = i;
        body->pdata = NULL;
        
        list->pnext=body;
        body->pprev=list;
      
        list=list->pnext;
    }
    return head;
}

PAGE_CONFIG* InsertTailNode(PAGE_CONFIG * head){
   
   
    PAGE_CONFIG *tmp = head;
    if(head == NULL)return 0;
        
    while((tmp->pnext) !=NULL)
    {
        tmp = tmp->pnext;
        nnn++;
    }
   
    PAGE_CONFIG *tailnode = (PAGE_CONFIG*)malloc(sizeof(PAGE_CONFIG));
    if(tailnode == NULL)return 0;
   
   
        tailnode->pdata = NULL;
        tailnode->temp = NULL;
        tailnode->page_n = nnn+1;
    tmp->pnext = tailnode;
    tailnode->pprev = tmp;
    tailnode->pnext = NULL;
   
    return head;
}


    Head = initLine(Head);
    current = Head->pnext;.
程序运行到InsertTailNode 函数内的    while((tmp->pnext) !=NULL) 就飞了, 应该是InsertTailNode 函数写的有些问题,求大神分析下



搜索更多相关主题的帖子: while struct NULL return head 
2021-08-19 10:23
我善治鬼
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:107
专家分:181
注 册:2015-2-16
收藏
得分:7 
链表结构其实是现实中自行车链条的仿真, 只有文字没有图形是很难描述清楚的,
你应该使用画图软件将链表每一步动作都画出来, 才能确定哪里出了问题
2021-08-19 11:19
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:7 
//https://www.bccn.net/run/

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

typedef unsigned char uint8_t;
typedef unsigned int  DRAW_PARA;

typedef struct {
    uint8_t title_num;
    DRAW_PARA *title;
    uint8_t content_num;
    DRAW_PARA *content;
}PAGE_DATA;

typedef struct page_config
{
    uint8_t page_n;
    PAGE_DATA *temp;
    char (*pdata)[40];
    struct page_config *pnext;
    struct page_config *pprev;
}PAGE_CONFIG;

PAGE_DATA page_data[10];

PAGE_CONFIG* initLine(PAGE_CONFIG *head)
{
  
    head = (PAGE_CONFIG*)malloc(sizeof(PAGE_CONFIG));
 
    head->pprev=NULL;
    head->pnext=NULL;

    PAGE_CONFIG *list=head;
//follow
    printf("head = %p next = %p\n", head, head->pnext);
    for (int i=0; i<5; i++) {
        PAGE_CONFIG *body=(PAGE_CONFIG*)malloc(sizeof(PAGE_CONFIG));
        body->pprev=NULL;
        body->pnext=NULL;
        body->temp = &page_data[i];
        body->page_n = i;
        body->pdata = NULL;
//follow
        printf("body = %p ", body);
        list->pnext = body;
        printf("pnext = %p ", list->pnext);
        
        body->pprev = list;
//follow      
        list=list->pnext;
        printf("list = %p\n", list);
    }
//follow
    printf("head = %p\n", head);
    return head;
}

PAGE_CONFIG* InsertTailNode(PAGE_CONFIG *head)
{
    int nnn = 0;
    PAGE_CONFIG *tmp = head;
    if(head == NULL)return 0;
      
    puts("enter InsertTailNode");
    printf("%p, %p\n", tmp, tmp->pnext);
//segment fault
    //tmp = tmp->pnext;

   
    while((tmp->pnext) != NULL) {
        tmp = tmp->pnext;
        nnn++;
    }
   
    printf("nnn = %d\n", nnn);
   
    PAGE_CONFIG *tailnode = (PAGE_CONFIG*)malloc(sizeof(PAGE_CONFIG));
    if(tailnode == NULL)return 0;
   
    tailnode->pdata = NULL;
    tailnode->temp = NULL;
    tailnode->page_n = nnn + 1;
    tmp->pnext = tailnode;
    tailnode->pprev = tmp;
    tailnode->pnext = NULL;

    puts("exit InsertTailNode");
    return head;
}

int main(int argc, char *argv[])
{
    PAGE_CONFIG head, *nhead;
    nhead = initLine(&head);

//follow
    printf("%p, %p\n", &head, nhead);
   
    //InsertTailNode(&head);
//follow
    InsertTailNode(nhead);
    puts("end of main!");
   
    return 0;
}

[此贴子已经被作者于2021-8-19 12:32编辑过]

2021-08-19 11:41
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
print result:

head = 0x15ec010 next = (nil)
body = 0x15ed050 pnext = 0x15ed050 list = 0x15ed050
body = 0x15ed080 pnext = 0x15ed080 list = 0x15ed080
body = 0x15ed0b0 pnext = 0x15ed0b0 list = 0x15ed0b0
body = 0x15ed0e0 pnext = 0x15ed0e0 list = 0x15ed0e0
body = 0x15ed110 pnext = 0x15ed110 list = 0x15ed110
head = 0x15ec010
0x7ffcbfd88020, 0x15ec010
enter InsertTailNode
0x15ec010, 0x15ed050
nnn = 5
exit InsertTailNode
end of main!
2021-08-19 12:33
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:7 
PAGE_CONFIG* initLine(PAGE_CONFIG * head){

    head=(PAGE_CONFIG*)malloc(sizeof(PAGE_CONFIG));

这一句就不对了吧,你修改 head 这个临时变量干什么?
2021-08-19 14:56
j15506271841
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2021-8-19
收藏
得分:0 
谢谢各位,代码我在网上抄的,是比较奇怪
2021-08-19 15:07
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct page_config
{
    int value;

    struct page_config* next;
    struct page_config* prev;

} PAGE_CONFIG;

void list_pushback( PAGE_CONFIG** phead, int value )
{
    PAGE_CONFIG* prev = NULL;
    PAGE_CONFIG** ptail = phead;
    for( ; *ptail; prev=*ptail, ptail=&prev->next );

    (*ptail) = malloc( sizeof(PAGE_CONFIG) );
    (*ptail)->value = value;
    (*ptail)->prev = prev;
    (*ptail)->next = NULL;
}

void list_print( PAGE_CONFIG* phead )
{
    for( ; phead; phead=phead->next )
        printf( "%d%c", phead->value, " \n"[phead->next==NULL] );
}

int main( void )
{
    PAGE_CONFIG* head = NULL;
    list_print( head );

    list_pushback( &head, 0 );
    list_print( head );

    list_pushback( &head, 1 );
    list_print( head );

    list_pushback( &head, 2 );
    list_print( head );
}
2021-08-19 15:17
快速回复:双向链表问题,debug 在while((tmp->pnext) !=NULL) stm32会重启
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043015 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved