What is safe? returning a structure or the pointer from a function

#include <iostream>

struct person_t{
        int age;
};

person_t get_person1(){
        person_t person;
        person.age = 10;
        return person;
}

person_t * get_person2(){
        person_t *person = new person_t;
        person->age = 20;
        return person;
}

int main(){
        person_t person1 = get_person1();
        person_t *person2 = get_person2();
        std::cout << person1.age << std::endl;
        std::cout << person2->age << std::endl;
        delete person2;
        return 0;
}

I want to know what is the safest way to return a structure from a function.

As in the answers to the questions in here and here, it is said that when you create a object as in get_person1() , that object will be destroyed after when it goes out of scope.

But when I search for "How to return a struct from function c++", it suggest me method one (with get_person1() ) (Example here). But I think that method will destroy the object after the function was called and I think method 2 is the safest.

Am I wrong here..? Or any opinion regarding this topic..?

Thank you!!


Use return by value for 3 reasons:

  • It makes your code readable
  • Your struct here is small (one int) so it is like returning an int. You can do that in C++, it is efficient
  • In C++17 (and most compiler before that) the cost of the intermediate copy of the object will be avoided. It is known as RVO. You need to put only one return in your get_person function.

  • it is said that when you create a object as in get_person1() , that object will be destroyed after when it goes out of scope.

    What is destroyed is the local object (ie: person inside get_persion1() ). What is returned is a copy of that object: a struct person_t is copied (it may be moved as well). So, it is safe.


    get_person2() is also safe, but consider using smart pointers instead of raw pointers :

    std::unique_ptr<person_t> get_person2(){
            auto person = std::make_unique<person_t>();
            // For pre-C++14
            // std::unique_ptr<person_t> person(new person_t);
            person->age = 20;
            return person;
    }
    

    That way, the caller to get_person2() doesn't have to call delete (it may forget to do so).


    Both approaches are equally safe - neither one causes undefined behavior, and the value set inside the function makes it back to the caller.

    The main difference is that the first approach copies the struct , while the second approach copies a pointer to struct . When the struct is tiny, such as in your example, there is no difference. When the struct becomes large, returning a pointer may save you some CPU cycles at the expense of additional memory allocation, so it is far from being a guaranteed win.

    Obviously, the second approach has another drawback in that one has to delete the struct eventually. The first approach is free from this requirement.

    链接地址: http://www.djcxy.com/p/96538.html

    上一篇: C ++和何时使用删除

    下一篇: 什么是安全的? 从函数返回一个结构或指针