鱼C论坛

 找回密码
 立即注册
查看: 1094|回复: 1

free()调用时,第二次调用会出现产生断点???(vs2019)求解,救救孩子吧!!!

[复制链接]
发表于 2021-12-13 21:05:26 | 显示全部楼层 |阅读模式
25鱼币
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 1000
struct people
{
        people* front;
        int n;
        people* next;
};
typedef people People;

void chushihua(People* a,int n);
int main()
{
        People a;
        People* p;
        People* q,*p1,*p2;
        a.next = NULL;
        a.front = NULL;
        a.n = 1;
        int N,M,K;
        int i=0, m,count=0;
        int shunxu[MAX];
        scanf_s("%d %d %d", &N, &M, &K);
        chushihua(&a, N);

        m = N;
        q = &a;
        p = &a;
       
        while (m!=1)
        {
                count = 0;
                while (count != M)
                {
                        p = q;
                        q=q->front;
                        count++;
                }
                p = q->next;
                printf("%p\n", p);
                shunxu[i] = p->n;
                p2 = p->front;
                p1 = p->next;
                p1->front = p2;
                p2->next = p1;
                count = 0;
                m--;
                i++;
                if (m != 1)
                        free(p);
                if (m == 1)
                {
                       
                        break;
                }
                p = q;
                while (count != K)
                {
                        p = q;
                        q = q->next;
                        count++;
                }
                p = q->front;
                printf("%p\n", p);
                shunxu[i] = p->n;
                p2 = p->front;
                p1 = p->next;
                p1->front = p2;
                p2->next = p1;
                if(m!=1)
                free(p);
               
                i++;
                m--;
               
        }
        shunxu[N - 1] = q->n;


        for (i = 0; i < N; i++)
        {
                printf("%d ", shunxu[i]);
        }
       

        return 0;







}

void chushihua(People* a, int n)
{
        int j;
        People* NEW;
        People* other;
        other = (People*)malloc(sizeof(people));
;
        for (j = 0; j < n-2; j++)
        {
                NEW = (People*)malloc(sizeof(People));
                NEW->front = NULL;
                NEW->next = NULL;

               
                if (a->next == NULL)
                {
                        a->next = NEW;
                        NEW->front = a;
                }
                else
                {
                        other->next = NEW;
                        NEW->front = other;
                }
                NEW->n = j+2;
                other = NEW;
               

        }
        NEW = (People*)malloc(sizeof(People)+1);
        NEW->front = other;
        other->next = NEW;
        NEW->next = a;
        a->front = NEW;
        NEW->n = n;
       
       



}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-14 07:55:46 | 显示全部楼层
本帖最后由 jhq999 于 2021-12-14 11:33 编辑
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 1000
struct people
{
        people* front;
        int n;
        people* next;
};
typedef people People;

void chushihua(People* a,int n);
int main()
{
        People a;
        People* p;
        People* q,*p1,*p2;
        a.next = NULL;
        a.front = NULL;
        a.n = 1;
        int N,M,K;
        int i=0, m,count=0;
        int shunxu[MAX];
        scanf_s("%d %d %d", &N, &M, &K);
        chushihua(&a, N);

        m = N;
        q = &a;
        p = &a;
       
        while (m!=1)
        {
                count = 0;
                while (count != M)
                {
                        p = q;
                        q=q->front;
                        count++;
                }
                p = q->next;
                printf("%p\n", p);
                shunxu[i] = p->n;
                p2 = p->front;
                p1 = p->next;
                p1->front = p2;
                p2->next = p1;
                count = 0;
                m--;
                i++;
                if (m != 1)
                        free(p);
                if (m == 1)
                {
                       
                        break;
                }
                p = q;
                while (count != K)
                {
                        p = q;
                        q = q->next;
                        count++;
                }
                p = q->front;
                printf("%p\n", p);
                shunxu[i] = p->n;
                p2 = p->front;
                p1 = p->next;
                p1->front = p2;
                p2->next = p1;
                if(m!=1&&p!=&a)///////////////不可能释放已经声明变量a的内存空间
                free(p);
               
                i++;
                m--;
               
        }
        shunxu[N - 1] = q->n;


        for (i = 0; i < N; i++)
        {
                printf("%d ", shunxu[i]);
        }
       

        return 0;







}

void chushihua(People* a, int n)
{
        int j;
        People* NEW;
        People* other;
        other = (People*)malloc(sizeof(people));
;
        for (j = 0; j < n-2; j++)
        {
                NEW = (People*)malloc(sizeof(People));
                NEW->front = NULL;
                NEW->next = NULL;

               
                if (a->next == NULL)
                {
                        a->next = NEW;
                        NEW->front = a;
                }
                else
                {
                        other->next = NEW;
                        NEW->front = other;
                }
                NEW->n = j+2;
                other = NEW;
               

        }
        NEW = (People*)malloc(sizeof(People)+1);
        NEW->front = other;
        other->next = NEW;
        NEW->next = a;
        a->front = NEW;
        NEW->n = n;
       
       



}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-18 20:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表