代码:
#include <iostream>
#include <string>
#include <fstream>
int main(int argc, char *argv[]) {
std::string filename = "dummyfile";
std::ifstream infile(filename);
std::string line;
std::getline(infile, line);
return 0;
}
编译并链接:
g++-7 -std=c++17 -flto mycode.cpp
结果:
*`./a.out'出错:free():无效指针:0x0000000000602200 * 已中止(核心转储)
它编译并在没有-flto
标志的情况下正常运行。保持-flto
但将-std=c++17
更改为-std=c++14
或-std=c++11
也会使其有效。因此,似乎有flto
和c++17
的组合。
gdb结果:
#0 0x00007ffff7a4acc9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1 0x00007ffff7a4e0d8 in __GI_abort () at abort.c:89
#2 0x00007ffff7a87f24 in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff7b966c8 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:175
#3 0x00007ffff7a941fe in malloc_printerr (ptr=<optimized out>, str=0x7ffff7b927b9 "free(): invalid pointer", action=1) at malloc.c:4996
#4 _int_free (av=<optimized out>, p=<optimized out>, have_lock=0) at malloc.c:3840
#5 0x00007ffff77c6e25 in std::string::reserve(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007ffff77c706a in std::string::append(char const*, unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x00007ffff7784aba in std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8 0x000000000040130b in std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) ()
#9 0x00000000004010cd in main ()
编译器和操作系统: g ++ - 7(Ubuntu 7.2.0-1ubuntu1~14.04)7.2.0