【问题标题】:"Uninitialised value was created by a stack allocation" in c++ std::mapc++ std::map 中的“未初始化的值是由堆栈分配创建的”
【发布时间】:2025-12-25 05:00:06
【问题描述】:

Valgrind 告诉我我的代码中有错误,但我找不到它... 这是一个代码sn-p:

22 int main(int argc, char *argv[]){
...
//argv[1] contains the name of a file
int length=atoi(argv[2]);
map<string, double> Map;
char* Key;
Key = new char [length+1];
...

我也使用了Key[length]='\0',但这似乎不会影响其余代码。

现在我用从文件中获取的条目填充映射(其中包含键和值行的列表。键的维度总是小于长度。

while( file_stream >> Key >> Value){
Map[Key]=Value;
....
}

此时,我调用:

cout << Key  << " has value ";
159   cout << Map[Key] << endl;

程序编译和执行都很好,但是 Valgrind 给出了很多这种类型的错误:

==6921== Conditional jump or move depends on uninitialised value(s)
==6921==    at 0x56274A0: __printf_fp (printf_fp.c:404)
==6921==    by 0x562396A: vfprintf (vfprintf.c:1622)
==6921==    by 0x5648C81: vsnprintf (vsnprintf.c:120)
==6921==    by 0x4EB64AE: ??? (in /usr/lib/libstdc++.so.6.0.13)
==6921==    by 0x4EB9002: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, char, double) const (in /usr/lib/libstdc++.so.6.0.13)
==6921==    by 0x4EB9328: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, double) const (in /usr/lib/libstdc++.so.6.0.13)
==6921==    by 0x4ECCC9E: std::ostream& std::ostream::_M_insert<double>(double) (in /usr/lib/libstdc++.so.6.0.13)
==6921==    by 0x40366E: main (MyProgram.cpp:159)
==6921==  Uninitialised value was created by a stack allocation
==6921==    at 0x401C61: main (MyProgram.cpp:22)

你有什么线索吗?我宁愿不发布所有长代码(请不要为此责备)

谢谢!!

【问题讨论】:

  • 为什么你有一个map&lt;string,double&gt;,然后使用动态分配的char* Key 而不是string Key
  • “你知道我为什么会犯这个错误吗” - 为什么你会犯这个错误?一点头绪都没有。很难进入那个思考过程。 发布真实可编译代码
  • @WhozCraig 你是对的,我编辑了这个问题。
  • @us2012 感谢您的提示,我将更正代码,看看会发生什么。
  • 请在此处和/或 ideone.com 发布 sscce

标签: c++ map std valgrind


【解决方案1】:

堆栈跟踪表明您正在格式化double。除非您明确地将未初始化的double 插入其中,否则无法从Map[Key] 获得未初始化的double(即使Map 中不存在Key,它将使用double() 初始化)。这可能指向__printf_fp() 的实现中的一段有趣的代码:我会用一个简单的测试程序验证该报告是否确实可以使用,例如:

#include <iostream>
int main() {
    std::cout << 3.14;
}

...如果是这样,我会忽略来自valgrind 的报告。在一个非常平静的下午,可能会仔细研究__printf_fp() 的实现,以找出它在哪里做了一些有问题的事情并确定它是否真的重要(不过,我不会赌这件事发生在我身上:我宁愿自己实现浮点格式化,但是,这可能需要一个下午以上的时间,因为这显然不简单)。

【讨论】:

  • 非常感谢。我无法读取 Valgrind 的输出,所以我将注意力集中在地图本身上,而不是值...但是那里有一个未初始化的值...