向各位大佬求教个问题:
1、我的程序用串口接收PC端的数据,发现接收到第四个时就无法在接收;仿真发现进入了HardFault_Handle()函数中;
2、command里面显示:
3、我的代码中是将接收到的串口数据保存到一个队列中,用到了动态内存分配:
结构体定义:
typedef union
{
float fv;
uint8_t cv[4];
}float_union;
typedef struct
{
unsigned char start_code1; //起始码1
unsigned char start_code2; //起始码2
unsigned char func_code; //功能码
unsigned char sending_num;
unsigned char this_sending_num;
float_union joint_pos[6];
float_union joint_vel[6];
float_union joint_acc[6];
float_union joint_t;
unsigned char crc_code;
}RosDataNodeStruct;
typedef struct RosData
{
RosDataNodeStruct data;
struct RosData *next;
}RosDataNode;
初始化:
RosDataNode* RosQueueInit()
{
RosDataNode* queue = (RosDataNode*)malloc(sizeof(RosDataNode));
queue->data = RosDataQueueNodeInit;
queue->next = NULL;
return queue;
}
/*数据入队列,返回队列新的尾指针*/
RosDataNode* RosDataEnterQueue(RosDataNode* rear,RosDataNodeStruct rosdata)
{
RosDataNode* enElem = (RosDataNode*)malloc(sizeof(RosDataNode)); //分配节点空间(动态内存分配)
enElem->data = rosdata; //节点数据保存
enElem->next = NULL; //节点指针指向NULL
rear->next = enElem; //新节点放到rear节点后
rear = enElem; //得到新的队尾
return rear; //返回新的队尾指针
}
/*数据出队列,返回队列新的头指针*/
RosDataNodeStruct RosDataOutQueue(RosDataNode* top,RosDataNode* rear)
{
RosDataNodeStruct temp = {0};
RosDataNode *p = {0};
if(top->next == NULL) //队列为空
{
printf("queue is empty"); //????????????
return temp;
}
p = top->next;
temp = p->data;
top->next = p->next; //头指针指向下一个节点
if(rear == p) //出队后只剩一个节点则rear=top
{
ros_rear = top;
}
free(p); //释放节点内存
return temp;
}
ros_queue=ros_top=ros_rear=RosQueueInit();
初始化后仿真发现rear->next有很多
问题:1、仿真出现的问题是越界了吗?2、为什么初始化后会有如此多的rear->next,不是应该只有一个空指针吗?会不会是这种情况导致的内存不够?谢谢!
博主您好,麻烦博主您看一下私信