cjl小学生 发表于 2020-6-28 10:22:40

结构体自引用问题

typedef struct QueueNode{               
        u8* data;
        struct QueueNode *next;
}QNode,*QueuePtr;

typedef struct Link_queue{
        QueuePtr front,rear;
}LinkQueue;

int Enqueue(LinkQueue* Q, u8* e, u16 len)
{
        int i;
        QueuePtr newNode = (QueuePtr)malloc(sizeof(QNode));
        newNode->data = (u8*)malloc(len);                  

        //Êý¾ÝÓòÑ­»·¸³Öµ
        for(i=0; i<len; i++)
        {
                newNode->data = e;
        }
        newNode->data = '\0';
        newNode->next = NULL;

        Q->rear->next = newNode;        //运行完这一句只有,进入硬件中断,注释掉之后可正常运行
        Q->rear = newNode;           
        return 0;
}

求大神解惑,为什么在Keil里这样运行,会进入硬件中断?在C编译器里面可正常运行。

cjl小学生 发表于 2020-6-28 10:43:55

有人吗?在线等

cjl小学生 发表于 2020-6-28 10:44:20

自顶

cjl小学生 发表于 2020-6-28 10:44:44

自顶

流水源 发表于 2020-6-28 14:34:00

第一:newNode->data = '\0';这里应该只能到len-1,总长度才len。
第二:
      Q->rear->next = newNode;      //运行完这一句只有,进入硬件中断,注释掉之后可正常运行
Q->rear指向的内存你都没确定分配,没指向具体有效内存存储,不能用Q->rear->next 。只能先Q->rear=取值,然后Q->rear->next。

陌路夕颜 发表于 2020-6-29 21:26:03

newNode->data = '\0'; 你这都越界了,QueuePtr front,rear;这只是个指针,没有分配内存,所以这句Q->rear->next会导致hardfalt,因为你非法操作
页: [1]
查看完整版本: 结构体自引用问题