我試圖理解編譯器警告。我有一個簡單的課程
#pragma once
#include <framework/project_definitions.hpp>
#include <framework/free_rtos/free_rtos.hpp>
#include <modules/net/imqtt_client.hpp>
#include "mqtt_command_base.hpp"
namespace application::commands
{
class check_alive_command : public mqtt_command_base
{
public:
check_alive_command(
modules::imqtt_client &mqtt_client,
framework::free_rtos::ifree_rtos &free_rtos);
int32_t execute(const uint8_t *payload, uint32_t length) override;
};
}
具有以下實作
#include "check_alive_command.hpp"
const static uint8_t _payload_buffer[10] = {};
namespace application::commands
{
check_alive_command::check_alive_command(
modules::imqtt_client &mqtt_client,
framework::free_rtos::ifree_rtos &free_rtos) :
mqtt_command_base(
mqtt_client,
free_rtos,
iobox_check_alive_command,
stack_size_small,
&_payload_buffer[0])
{
}
int32_t check_alive_command::execute(const uint8_t *payload, uint32_t length)
{
return modules::mqtt_error_code::ok;
}
}
的基礎check_alive_command
#pragma once
#include <modules/net/imqtt_client.hpp>
#include <framework/free_rtos/free_rtos.hpp>
namespace valinso::application::commands
{
class mqtt_command_base
{
public:
mqtt_command_base(
modules::imqtt_client &mqtt_client,
framework::free_rtos::ifree_rtos &free_rtos,
const char * command_name,
const uint32_t stack_size,
uint8_t *payload_buffer);
void run(const uint8_t *payload, uint32_t length);
virtual int32_t execute(const uint8_t *payload, uint32_t length) = 0;
protected:
modules::imqtt_client &_mqtt_client;
framework::free_rtos::ifree_rtos &_free_rtos;
const char * _command_name;
const uint32_t _stack_size;
uint8_t *_payload_buffer;
size_t _current_payload_length = 0;
bool _is_busy = false;
};
}
編譯器警告
/home/bp/dev/iobox/firmware/app/iobox/commands/check_alive_command.cpp: In constructor 'application::commands::check_alive_command::check_alive_command(modules::imqtt_client&, framework::free_rtos::ifree_rtos&)':
/home/bp/dev/iobox/firmware/app/iobox/commands/check_alive_command.cpp:15:18: warning: '*<unknown>.application::commands::check_alive_command::<anonymous>.application::commands::mqtt_command_base::_payload_buffer' is used uninitialized in this function [-Wuninitialized]
15 | &_payload_buffer[0])
為什么?如何??這甚至不是真的,對吧?初始化.data部分的啟動代碼在我的__libc__init_array. 那么為什么會未初始化呢?更重要的是,我該如何解決這個警告?個人更重要:為什么會發出這個警告?
uj5u.com熱心網友回復:
這很簡單,真的。您有兩個名為_payload_buffer. 一個是你的靜態常量,另一個是mqtt_command_base's 成員之一。
當您參考_payload_bufferincheck_alive_command的建構式時,您實際上是在使用成員,而不是靜態變數。所以當然,此時它是未初始化的。你的編譯器是對的。
至于修復,要么重命名這兩個中的一個,要么告訴編譯器你想要全域的:::
mqtt_command_base(
mqtt_client,
free_rtos,
iobox_check_alive_command,
stack_size_small,
&::_payload_buffer[0]) // <-- :: right there
由你決定。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/515295.html
標籤:C
上一篇:無法將數字添加到陣列中的現有索引
