#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!!
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.
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.
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 .
No, If a function return type is declared as void it cannot return any value.
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.
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 ...
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.
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.
Use return by value for 3 reasons:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With