Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:05:15+00:00 2026-06-13T23:05:15+00:00

Possible Duplicate: how to return an object in C++ Hello, guys! If I need

  • 0

Possible Duplicate:
how to “return an object” in C++

Hello, guys!

If I need to return an object from function (and it is not a getter, and also this function for some reason cannot be implemented as a constructor, for example there is a constructor with the same signature and different semantics), how better to implement this?

For example, one can propose the following options:

1) Return an object itself:

MyClass GetMyClass() {
    MyClass a;
    ...
    return a;
}

In most cases RVO, http://en.wikipedia.org/wiki/Return_value_optimization is performed, no copy constructors is called and there is no overhead.
The drawback of this approach is that despite RVO, you must specify the copy construtor of MyClass, but as mentioned in Google C++ Style Guide…

Implicit copying of objects in C++ is a rich source of bugs and of performance problems.

2) Return a pointer to an object:

MyClass* GetMyClass() {
    MyClass* a= new MyClass();
    ...
    return a;
}

In such case, there is an overhead because of allocating memory on heap. This somehow slows down the program due to malloc system call.
Also, you should free memory outside of this function, which means that allocating and releasing performed in different logical units. As mentioned here, Why malloc memory in a function and free it outside is a bad idea? it is more often a bad idea.

3) Pass value by reference or by pointer:

void GetMyClass(MyClass& a) {
    ...
}

In this case, code becomes ugly and poorly readable

What is the best practice?
Which one do you use in your projects and which criteria are the most important when you are working on a big project?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:05:16+00:00Added an answer on June 13, 2026 at 11:05 pm

    Two is pretty straightforward. Yes, malloc can be slow, but does it matter in your case? You need space for that object from somewhere. Also note the answers in the question you point to, it’s only across logical units that there is an issue. See if you have the problem and then try to fix it rather than pre-optimizing.

    You could potentially make your own version of new that uses a free store and managers resources itself. However, unless you’ve measured and found new to be a problem it’s error prone.

    One way you can help ameliorate problems with freeing pointers is by using a std::shared_ptr. That way you don’t have to worry about freeing the object as long as you always use it through the smart pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Return Value from inside of $.ajax() function I'm working on a CakePHP
Possible Duplicate: What is the proper way to return an object from a C++
Possible Duplicate: Return value from thread I want to get the free memory of
Possible Duplicate: To return IQueryable<T> or not return IQueryable<T> I have LINQ to SQL
Possible Duplicate: c++ virtual function return type I have a simple but confusing question
Possible Duplicate: Fetch data in database table insert it if not exist else return
Possible Duplicate: How do I test for an empty Javascript object from JSON? Is
Possible Duplicate: Safest Way to Loop Over Javascript Object I have this hard coded
Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? This is known
Possible Duplicate: Why is object not dealloc'ed when using ARC + NSZombieEnabled I've got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.