c++ Questions about Extinction Relevant

Asked 2 years ago, Updated 2 years ago, 24 views

// Enter code here
#include <iostream>

class Point {
public:
    char * name_;
    int age_;
    Point(){};
    Point(char * name,int age) {
        name_ = new char[strlen(name)+1];
        strcpy(name_,name);
        age_=age;
    }

    Point(const Point &p) {
        name_ = new char[strlen(p.name_)+1];
        strcpy(name_,p.name_);
        age_=p.age_;
    }

    ~Point(){
        std::cout<<b<<"start"<<std::endl;
        delete [] name_;
        std::cout<<b++<<"end"<<std::endl;
    }
};

int main(void)
{
    Point p1("C++",20);
    Point p2=p1;
    //Point * p3 = &p1;
    Point & p4 = p1;

    char * name1 = "hello";
    char *& name2 = name1;





    std::cout<<"----------------------------"<<std::endl;


    std::cout<<p1.name_<<std::endl;
    std::cout<<p2.name_<<std::endl;
    //std::cout<<(p3->name_)<<std::endl;
    std::cout<<p4.name_<<std::endl;


    std::cout<<"----------------------------"<<std::endl;
    //p3->name_ = "Hello";


    std::cout<<p1.name_<<std::endl;
    std::cout<<p2.name_<<std::endl;
    //std::cout<<(p3->name_)<<std::endl;
    std::cout<<p4.name_<<std::endl;


    std::cout<<"----------------------------"<<std::endl;
    p4.name_ = "World";


    std::cout<<p1.name_<<std::endl;
    std::cout<<p2.name_<<std::endl;
    //std::cout<<(p3->name_)<<std::endl;
    std::cout<<p4.name_<<std::endl;

    std::cout<<"----------------------------"<<std::endl;
    std::cout<<&name1<<std::endl;
    std::cout<<&name2<<std::endl;
    return 0;
}

I'm studying c++ while doing Java. I made a code because I was curious about something while studying through a book, but I think there is a runtime error due to p3 and p4. Does the pointer variable and the reference variable also fail to find the dynamically allocated memory because the destructor is called at the end of the program? Or I wonder if there is another reason. The printout of the extinction is Start1end1start2. Or did I write the wrong code?

c++

2022-09-22 19:58

1 Answers

There is a garbage collector in Java has garbage collectors. The advantage of Java is that it has this garbage collector, so memory errors rarely occur. In addition, the lack of grammatical pointers makes it impossible to handle memory directly, which also contributes to stable service development.

There is no garbage collector on c++, so you need to be careful about using memory.

For Java developers to handle c/c++ well, memory usage must be used with great care. It may require a lot of learning.

To give you a quick tip, in c++, use string instead of char * and smart pointer instead of new or delete.

Use stl for data structure.

To give you a simple answer to the error...

In the code of the question, the address of a new place, such as p4.name_="World";, appears to be substituted.

name_ = not a memory address value for new char[strlen(name)+1];

For verification

Back up the name_address in the main function as shown below and restore it again before return.

Point & p4 = p1;
char *temp = p1.name_; // name_ Back up address
...
...
p1.name_ = temp;
return 0;

This will not cause segment fault.


2022-09-22 19:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.