小菠菜789 2022-04-26 17:33 采纳率: 100%
浏览 26
已结题

malloc无法分配内存

为什么malloc无法正常分配内存


#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct
{
    QElemType *base;
    int front;
    int rear;
}SqQueue;

Status InitQueue(SqQueue *Q,int n)
{
    Q->base= (QElemType *)malloc(n*sizeof(QElemType));
    if(!Q->base) exit(OVERFLOW);
    Q->front=Q->rear=0;
    return OK;
}

//int QueueLength(SqQueue *Q)
//{
//    return (Q->rear-Q->front+MAXSIZE)%MAXSIZE;
//}

Status EnQueue(SqQueue *Q,QElemType e,int n)
{
    if((Q->rear+1)%n==Q->front) return ERROR;
    Q->base[Q->rear]=e;
    Q->rear=(Q->rear+1)%n;
    return OK;
}
// Status Queue(SqQueue *Q){
//     while
// }
void DeQueue(SqQueue *Q,QElemType *e,int n)
{
    while(Q->base[Q->front]=0){
        Q->front=(Q->front+1)%n;
    }
    *e=Q->base[Q->front];
    Q->front=(Q->front+1)%n;
}

//Status GetHead(SqQueue *Q,QElemType *e)
//{
//    if(Q->rear==Q->front) return ERROR;
//    *e=Q->base[Q->front];
//    return OK;
//}

void DestroyQueue(SqQueue *Q)
{
    if(Q->base) free(Q->base);
    Q->base=NULL;
    Q->front=0;
    Q->rear=0;
}

int main()
{
    SqQueue *Q;
    //自行编写代码
    int n;
    scanf("%d",&n);
    int m;
    scanf("%d",&m);
    InitQueue(Q,n);
    printf("+");
    for (int i=1;i<=n;i++){
        EnQueue(Q,i,n);
    }
    int e;
    int k=n;
    while(k!=1){
        int f;
        for(int j=0;j<m;j++){
            DeQueue(Q,&f,n);
        }
        Q->base[Q->front]=0;
        k--;
    }
    DeQueue(Q,&e,n);
    printf("%d",e);
    return 0;
}
  • 写回答

1条回答 默认 最新

  • 慕雪华年 C/C++领域新星创作者 2022-04-26 19:03
    关注

    两个问题

    • 判断相等写成了单个=
    • 初始化方式错了

    这里判断相等写错了

    img


    main函数中初始化方式错了

    img


    对应的访问方式也需要更改,->用于结构体指针,.用于结构体变量
    所有函数里面的Q都需要改成&Q

    img


    修改完毕后,代码可以运行了,但是有没有逻辑错误我不知道

    如果对你有帮助,还请点个采纳,万分感谢!

    #include <stdio.h>
    #include <stdlib.h>
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW -2
    typedef int Status;
    typedef int QElemType;
    typedef struct SqQueue
    {
        QElemType* base;
        int front;
        int rear;
    }SqQueue;
    
    Status InitQueue(SqQueue* Q, int n)
    {
        Q->base = (QElemType*)malloc(n * sizeof(QElemType));
        if (!Q->base) exit(OVERFLOW);
        Q->front = Q->rear = 0;
        return OK;
    }
    
    //int QueueLength(SqQueue *Q)
    //{
    //    return (Q->rear-Q->front+MAXSIZE)%MAXSIZE;
    //}
    
    Status EnQueue(SqQueue* Q, QElemType e, int n)
    {
        if ((Q->rear + 1) % n == Q->front) return ERROR;
        Q->base[Q->rear] = e;
        Q->rear = (Q->rear + 1) % n;
        return OK;
    }
    // Status Queue(SqQueue *Q){
    //     while
    // }
    void DeQueue(SqQueue* Q, QElemType* e, int n)
    {
        while (Q->base[Q->front] == 0) {
            Q->front = (Q->front + 1) % n;
        }
        *e = Q->base[Q->front];
        Q->front = (Q->front + 1) % n;
    }
    
    //Status GetHead(SqQueue *Q,QElemType *e)
    //{
    //    if(Q->rear==Q->front) return ERROR;
    //    *e=Q->base[Q->front];
    //    return OK;
    //}
    
    void DestroyQueue(SqQueue* Q)
    {
        if (Q->base) free(Q->base);
        Q->base = NULL;
        Q->front = 0;
        Q->rear = 0;
    }
    
    int main()
    {
        SqQueue Q;
        //自行编写代码
        int n;
        scanf("%d", &n);
        int m;
        scanf("%d", &m);
        InitQueue(&Q, n);
        printf("+");
        for (int i = 1; i <= n; i++) {
            EnQueue(&Q, i, n);
        }
        int e;
        int k = n;
        while (k != 1) {
            int f;
            for (int j = 0; j < m; j++) {
                DeQueue(&Q, &f, n);
            }
            Q.base[Q.front] = 0;
            k--;
        }
        DeQueue(&Q, &e, n);
        printf("%d", e);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月4日
  • 已采纳回答 4月26日
  • 创建了问题 4月26日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效