首先,我真的检查了是否已经有人问过一个问题,但我找不到任何问题。错误消息不应该欺骗你我的情况有点不同我猜或者我只是错过了一些东西。
当我处理一个玩具 C++ 代码时,我遇到了一个奇怪的错误。程序输出说有双重空闲情况,但我看不到发生此错误的地方。代码可能看的有点长,对此我深表歉意。
我现在正在工作Linux Distribution,我正在使用g++ 9.1.0. 我检查了我的代码并寻找错误的部分。
即使我固定的一些代码的一部分,我的问题没有得到解决,当我发表评论或者除了Foo{1, "Hello World"};或者vec.push_back(std::move(Foo{}));,我不为什么得到它。
class Foo
{
public:
Foo()
: val{nullptr}, str{nullptr}
{
std::cout << "You are in empty constructor\n";
}
Foo(int the_val, const char *the_str)
: val{new int}, str{new char[std::strlen(the_str + 1)]}
{
*val = the_val;
std::cout << *val << '\n';
std::strcpy(str, the_str);
std::cout << str << '\n';
}
~Foo()
{
if (val) {
delete val;
} else {
std::cout << "val is empty\n"; …Run Code Online (Sandbox Code Playgroud) c++ move-constructor construct copy-assignment move-assignment-operator