Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!

like image 680
Ramesh-X Avatar asked Feb 10 '18 09:02

Ramesh-X


People also ask

Can you return structure pointer from function?

We can pass pointers to the function as well as return pointer from a function. But it is not recommended to return the address of a local variable outside the function as it goes out of scope after function returns.

Should I return a struct or a pointer?

There are two ways of "returning a structure." You can return a copy of the data, or you can return a reference (pointer) to it. It's generally preferred to return (and pass around in general) a pointer, for a couple of reasons. First, copying a structure takes a lot more CPU time than copying a pointer.

How a function can return a structure?

Return struct from a function Here, the getInformation() function is called using s = getInformation(); statement. The function returns a structure of type struct student . The returned structure is displayed from the main() function. Notice that, the return type of getInformation() is also struct student .

Is it possible to return a pointer from a function yes or no?

No, If a function return type is declared as void it cannot return any value.

Can a function return a pointer to data?

We have already seen a function can return data of types int , float, char etc. Similarly, a function can return a pointer to data. The syntax of a function returning a pointer is as follows.

How to pass a structure to a function?

Passing of structure to the function can be done in two ways: 1 By passing all the elements to the function individually. 2 By passing the entire structure to the function. More ...

What are pointers in C programming language?

Pointers in C programming language is a variable which is used to store the memory address of another variable. We can pass pointers to the function as well as return pointer from a function. But it is not recommended to return the address of a local variable outside the function as it goes out of scope after function returns.

Is it possible to return a studenttype from a function?

One more thing, even if you do that, do not create a local studentType instance inside the function. The instance is on the function stack and will not be available when you try to return it. One thing you can however do is create studentType dynamically and return the pointer to it outside the function.


2 Answers

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.
like image 101
Gabriel Avatar answered Sep 19 '22 13:09

Gabriel


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 (i.e.: 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).

like image 35
ネロク・ゴ Avatar answered Sep 17 '22 13:09

ネロク・ゴ