Run-Time Check Failure #3 - The variable 'comstat' is being used without being initialized

本文介绍了三种解决Run-Time Check Failure #3错误的方法:修改项目配置属性以设置基本运行时检查为默认;将未初始化变量`comstat`改为静态变量`static COMSTAT comstat`;以及在代码中使用`memset`初始化`comstat`。这些方案针对Debug和Release模式下的编译问题进行了说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方案1: 改变项目配置属性


一种解决方案是改变基本运行时检查(changing the runtime checks in project settings):在菜单Project->Project properties-> C/C++ -> Code generation-> Basic Runtime checks --> change to 'Default',在中文版中是:项目-〉属性-〉配置属性-〉C/C++代码生成-〉基本运行时检查-〉设置为默认,当将基本运行时检查改为默认之后,编译自然顺利通过了,向串口调试助手发送消息正常了,再从串口调试助手向SerialPortTest发送消息时,也正常了。


方案

auto start_time1 = std::chrono::steady_clock::now(); if (all_serial[index].isconnected() == -1) { log_error("Please open the come port!"); return -1; } int transmission_times = 1; memset(&parsered_message[index], 0, 128); memset(&packed_message[index], 0, 128); uint8_t head = heads[slave]; uint8_t packed_length = 0; package_protocol_message(slave, send, (uint8_t*)(packed_command+index), sendLength, &packed_length, mode); RETRY: all_serial[index].getBufferBytes(); //清除发送缓冲区和接收缓冲区 int32_t result = (int32_t)all_serial[index].sendArray(packed_command[index], packed_length);//发送相关指令 if (result != 0) { log_error("%s:Failed to send command to sensor.", send); return result; } //倘若是修改baud率,则不用接受数据 if (sendLength > 6 && (memcmp(send,"baud",4)==0)) { return 0; } //倘若是修改load,则超时 int timeout_multi = 1; if (memcmp(send,"load",4) == 0 || memcmp(send,"frmt",4) == 0) { timeout_multi = 50; } //开始接收协议头数据 _Bool timeout = true;//是否会超时 uint32_t error; COMSTAT comstat;//串口状态 uint32_t actual_length;//串口接收数据的实际字计数 auto start_time = std::chrono::steady_clock::now(); while (ELAPSED_TIME(start_time) <= TIME_OUT_MS*timeout_multi) { result = (int32_t)all_serial[index].clearcom(&error, &comstat); if (comstat.cbInQue < PACKET_EXTRA_HEAD_SIZE) { continue; } result = (int32_t)all_serial[index].getArray(receive, PACKET_EXTRA_HEAD_SIZE, &actual_length);//接收数据 if (actual_length == PACKET_EXTRA_HEAD_SIZE) { timeout = false; break; } } //超时 if (timeout) { log_error(" %s:receive header and length time out!", send); if (transmission_times <= RETRANSMISSION_TIMES) { transmission_times++; goto RETRY; } else { return -1; } } //crc校验错误 if (receive[0] == 1 && receive[1]==0) { log_error(" %s:crc error!", send); if (transmission_times < RETRANSMISSION_TIMES) { transmission_times++; goto RETRY; } else { return -1; } }这一段主要耗时操作在哪
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值