可变变量的奇怪值和覆盖文本
Weird value and overriding text on variadics
我正在尝试创建一个日志系统,您可以在其中将日志字符串推送到一个向量中,然后通过循环遍历该向量来打印所有日志,但似乎存在一个问题,我的向量中的字符串正在获取替换为最近推送的字符串,以及添加的奇怪字符。
struct color {
color(unsigned int nr, unsigned int ng, unsigned int nb, unsigned int na) : r(nr), g(ng), b(nb), a(na) {}
unsigned int r, g, b, a;
};
struct info {
const char* text;
float time;
color col;
info(const char* ntext, float ntime, color ncol) : text(ntext), time(ntime), col(ncol) { }
};
class event_logger {
public:
std::vector<info> info_list;
void add_log_messasge(color col, const char* message, ...) {
char fmsg[512] = { '[=10=]' };
va_list list;
va_start(list, message);
vsprintf_s(fmsg, 512, message, list);
va_end(list);
this->info_list.push_back(info(fmsg, 10.0f, col));
// limit to 12 messages at a time
if (this->info_list.size() > 12)
this->info_list.pop_back();
}
void print_log_info() {
/* Don't run if the vector is empty. */
if (!info_list.empty())
{
for (size_t i = 0; i < info_list.size(); i++)
{
printf_s("%s\n", info_list[i].text);
}
}
}
};
int main() {
event_logger log;
log.add_log_messasge(color(255, 255, 255, 255), "welcome (%.3f, %s)", 3.14f, "WHAT THE FUCK");
log.add_log_messasge(color(255, 255, 255, 255), "unlucky %s", "dude");
log.print_log_info();
}
有人知道问题出在哪里吗?
你的结构
struct info {
const char* text;
float time;
color col;
info(const char* ntext, float ntime, color ncol) : text(ntext), time(ntime), col(ncol) { }
};
正在将地址复制到局部变量 fmsg
。 fmsg
的生命周期限制在 add_log_messasge
的范围内(你打错了)。
存储局部变量的地址并在其范围之外使用它是未定义的行为。
使用 std::string
通过复制来存储 fmsg
的值,它会起作用。
struct info {
std::string text;
float time;
color col;
info(const char* ntext, float ntime, color ncol) : text(ntext), time(ntime), col(ncol) { }
};
您需要使用 c_str()
更改打印方式
void print_log_info() {
/* Don't run if the vector is empty. */
if (!info_list.empty())
{
for (size_t i = 0; i < info_list.size(); i++)
{
printf_s("%s\n", info_list[i].text.c_str());
}
}
}
注意:您使用 empty
的检查并不是真正必要的,因为 size
returns 0 的空向量调用永远不会启动您的 for 循环(i=0 can't be lower of size 0
)
我正在尝试创建一个日志系统,您可以在其中将日志字符串推送到一个向量中,然后通过循环遍历该向量来打印所有日志,但似乎存在一个问题,我的向量中的字符串正在获取替换为最近推送的字符串,以及添加的奇怪字符。
struct color {
color(unsigned int nr, unsigned int ng, unsigned int nb, unsigned int na) : r(nr), g(ng), b(nb), a(na) {}
unsigned int r, g, b, a;
};
struct info {
const char* text;
float time;
color col;
info(const char* ntext, float ntime, color ncol) : text(ntext), time(ntime), col(ncol) { }
};
class event_logger {
public:
std::vector<info> info_list;
void add_log_messasge(color col, const char* message, ...) {
char fmsg[512] = { '[=10=]' };
va_list list;
va_start(list, message);
vsprintf_s(fmsg, 512, message, list);
va_end(list);
this->info_list.push_back(info(fmsg, 10.0f, col));
// limit to 12 messages at a time
if (this->info_list.size() > 12)
this->info_list.pop_back();
}
void print_log_info() {
/* Don't run if the vector is empty. */
if (!info_list.empty())
{
for (size_t i = 0; i < info_list.size(); i++)
{
printf_s("%s\n", info_list[i].text);
}
}
}
};
int main() {
event_logger log;
log.add_log_messasge(color(255, 255, 255, 255), "welcome (%.3f, %s)", 3.14f, "WHAT THE FUCK");
log.add_log_messasge(color(255, 255, 255, 255), "unlucky %s", "dude");
log.print_log_info();
}
有人知道问题出在哪里吗?
你的结构
struct info {
const char* text;
float time;
color col;
info(const char* ntext, float ntime, color ncol) : text(ntext), time(ntime), col(ncol) { }
};
正在将地址复制到局部变量 fmsg
。 fmsg
的生命周期限制在 add_log_messasge
的范围内(你打错了)。
存储局部变量的地址并在其范围之外使用它是未定义的行为。
使用 std::string
通过复制来存储 fmsg
的值,它会起作用。
struct info {
std::string text;
float time;
color col;
info(const char* ntext, float ntime, color ncol) : text(ntext), time(ntime), col(ncol) { }
};
您需要使用 c_str()
void print_log_info() {
/* Don't run if the vector is empty. */
if (!info_list.empty())
{
for (size_t i = 0; i < info_list.size(); i++)
{
printf_s("%s\n", info_list[i].text.c_str());
}
}
}
注意:您使用 empty
的检查并不是真正必要的,因为 size
returns 0 的空向量调用永远不会启动您的 for 循环(i=0 can't be lower of size 0
)