Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C+;中,未定义的新运算符如何导致未定义的行为+;? 这里有一个C++代码: #include <iostream> #include <map> #include <string> #include <cstdlib> using namespace std; class Person { private: int year; Person(const Person& pers); public: Person(int y): year(y) { cout << "Default constructor" << endl;} ~Person() { cout << "Destructor " << endl; } int get_year() const { return year; } }; int main() { map<string, Person*> test; test.insert(pair<string, Person*>("ini_1", new Person(2))); return 0; } 从输出中,我想知道如何删除testmap-givennewperson(2)的值,而不首先对其进行编码_C++_New Operator_Heap Memory_Copy Constructor_Delete Operator - Fatal编程技术网

在C+;中,未定义的新运算符如何导致未定义的行为+;? 这里有一个C++代码: #include <iostream> #include <map> #include <string> #include <cstdlib> using namespace std; class Person { private: int year; Person(const Person& pers); public: Person(int y): year(y) { cout << "Default constructor" << endl;} ~Person() { cout << "Destructor " << endl; } int get_year() const { return year; } }; int main() { map<string, Person*> test; test.insert(pair<string, Person*>("ini_1", new Person(2))); return 0; } 从输出中,我想知道如何删除testmap-givennewperson(2)的值,而不首先对其进行编码

在C+;中,未定义的新运算符如何导致未定义的行为+;? 这里有一个C++代码: #include <iostream> #include <map> #include <string> #include <cstdlib> using namespace std; class Person { private: int year; Person(const Person& pers); public: Person(int y): year(y) { cout << "Default constructor" << endl;} ~Person() { cout << "Destructor " << endl; } int get_year() const { return year; } }; int main() { map<string, Person*> test; test.insert(pair<string, Person*>("ini_1", new Person(2))); return 0; } 从输出中,我想知道如何删除testmap-givennewperson(2)的值,而不首先对其进行编码,c++,new-operator,heap-memory,copy-constructor,delete-operator,C++,New Operator,Heap Memory,Copy Constructor,Delete Operator,输出: constructor copy constructor copy constructor Destructor Destructor Destructor 我不明白为什么构造函数运行了一次,复制构造函数运行了两次。你能解释一下他们发生在哪里吗 谢谢 从输出中,我可以看到析构函数没有运行。我想 知道吗,我如何在不定义新指针的情况下删除它 你申报了一张地图;指向某人的指针 map<string, Person*> test; 如果不删除分配的对象,则会出现内存泄漏 我不明白

输出:

constructor
copy constructor
copy constructor
Destructor
Destructor
Destructor
  • 我不明白为什么构造函数运行了一次,复制构造函数运行了两次。你能解释一下他们发生在哪里吗 谢谢

    从输出中,我可以看到析构函数没有运行。我想 知道吗,我如何在不定义新指针的情况下删除它

    你申报了一张地图;指向某人的指针

    map<string, Person*> test;
    
    如果不删除分配的对象,则会出现内存泄漏

    我不明白为什么构造器只运行一次并复制 构造函数运行了两次。你能解释一下他们发生在哪里吗

    在这份声明中

    test.insert(pair<string, Person*>("ini_1", new Person(2)));
    
    test.insert(pair<string, Person>("ini_1", Person(2)));
    
    test.insert(成对(“ini_1”,人(2));
    
    显式调用转换构造函数来创建Person
    Person(2)
    类型的对象

    然后调用类Person的copy构造函数来创建类型为
    pair
    的对象

    最后,再次将该对象复制到映射中,并为数据成员对的第二个调用Person类型的复制构造函数


    因此创建了三个对象,并调用了三个对象的析构函数。

    听起来您可以使用一个我看不到任何未定义的行为。在指针情况下,析构函数不会运行,但这是因为没有
    delete
    。您可以通过使用智能指针而不是原始指针来纠正这一点。但是使用值而不是指针并没有什么错。@wangmyde您不能测试未定义的行为,您必须知道。C++中没有“新指针”或“未定义指针”,如果你想被理解,你应该使用常用术语。4。您创建了
    Person
    temporary,它被复制来构造
    temporary,并再次复制到insert函数中。@wangmyde,类似于执行
    doSomething(2)而不是
    inti=2;剂量测定法(i)-也就是说,这没有任何区别。嗨,来自莫斯科的@Vlad。如果我使用
    test.insert(成对(“ini_1”,新的人(2))但不
    人员*per=新人员(2);测试。插入(成对(“ini_1”,每根)),是否会导致未定义的行为?就像@h0r53所说的那样,使用new几乎肯定会导致问题……
    使用new几乎肯定会导致问题,因为Person对象将存储在堆分配的内存中。1) 除非对对象调用delete,否则将出现内存泄漏。2) 即使您确实删除了对象,它分配的堆内存也可以重用,而您的映射仍然指向该内存。更糟糕的是。
    对吗?@wangmyde在这种情况下,未定义的行为都不会出现。如果使用本地声明的指针和存储在映射中的指针尝试删除内存两次,则可能会发生未定义行为。
    constructor
    copy constructor
    copy constructor
    Destructor
    Destructor
    Destructor
    
    map<string, Person*> test;
    
    test.insert(pair<string, Person*>("ini_1", new Person(2)));
    
    for ( auto &item : test )
    {
        delete item.second;
         item.second = nullptr;
    }
    
    test.insert(pair<string, Person>("ini_1", Person(2)));