鱼C论坛

 找回密码
 立即注册
查看: 1442|回复: 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;
       
       



}

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-14 07:55:46 | 显示全部楼层
本帖最后由 jhq999 于 2021-12-14 11:33 编辑

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #define MAX 1000
  5. struct people
  6. {
  7.         people* front;
  8.         int n;
  9.         people* next;
  10. };
  11. typedef people People;

  12. void chushihua(People* a,int n);
  13. int main()
  14. {
  15.         People a;
  16.         People* p;
  17.         People* q,*p1,*p2;
  18.         a.next = NULL;
  19.         a.front = NULL;
  20.         a.n = 1;
  21.         int N,M,K;
  22.         int i=0, m,count=0;
  23.         int shunxu[MAX];
  24.         scanf_s("%d %d %d", &N, &M, &K);
  25.         chushihua(&a, N);

  26.         m = N;
  27.         q = &a;
  28.         p = &a;
  29.       
  30.         while (m!=1)
  31.         {
  32.                 count = 0;
  33.                 while (count != M)
  34.                 {
  35.                         p = q;
  36.                         q=q->front;
  37.                         count++;
  38.                 }
  39.                 p = q->next;
  40.                 printf("%p\n", p);
  41.                 shunxu[i] = p->n;
  42.                 p2 = p->front;
  43.                 p1 = p->next;
  44.                 p1->front = p2;
  45.                 p2->next = p1;
  46.                 count = 0;
  47.                 m--;
  48.                 i++;
  49.                 if (m != 1)
  50.                         free(p);
  51.                 if (m == 1)
  52.                 {
  53.                        
  54.                         break;
  55.                 }
  56.                 p = q;
  57.                 while (count != K)
  58.                 {
  59.                         p = q;
  60.                         q = q->next;
  61.                         count++;
  62.                 }
  63.                 p = q->front;
  64.                 printf("%p\n", p);
  65.                 shunxu[i] = p->n;
  66.                 p2 = p->front;
  67.                 p1 = p->next;
  68.                 p1->front = p2;
  69.                 p2->next = p1;
  70.                 if(m!=1&&p!=&a)///////////////不可能释放已经声明变量a的内存空间
  71.                 free(p);
  72.                
  73.                 i++;
  74.                 m--;
  75.                
  76.         }
  77.         shunxu[N - 1] = q->n;


  78.         for (i = 0; i < N; i++)
  79.         {
  80.                 printf("%d ", shunxu[i]);
  81.         }
  82.       

  83.         return 0;







  84. }

  85. void chushihua(People* a, int n)
  86. {
  87.         int j;
  88.         People* NEW;
  89.         People* other;
  90.         other = (People*)malloc(sizeof(people));
  91. ;
  92.         for (j = 0; j < n-2; j++)
  93.         {
  94.                 NEW = (People*)malloc(sizeof(People));
  95.                 NEW->front = NULL;
  96.                 NEW->next = NULL;

  97.                
  98.                 if (a->next == NULL)
  99.                 {
  100.                         a->next = NEW;
  101.                         NEW->front = a;
  102.                 }
  103.                 else
  104.                 {
  105.                         other->next = NEW;
  106.                         NEW->front = other;
  107.                 }
  108.                 NEW->n = j+2;
  109.                 other = NEW;
  110.                

  111.         }
  112.         NEW = (People*)malloc(sizeof(People)+1);
  113.         NEW->front = other;
  114.         other->next = NEW;
  115.         NEW->next = a;
  116.         a->front = NEW;
  117.         NEW->n = n;
  118.       
  119.       



  120. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 06:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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