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 6045003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:04:35+00:00 2026-05-23T07:04:35+00:00

Which way is good for returning an array of classes? class BigSizeClass { int

  • 0

Which way is good for returning an array of classes?

class BigSizeClass {
    int data[1024 * 1024];
};
  1. Very bad and expensive way:

    CArray<BigSizeClass> test() {
      CArray<BigSizeClass> ret;
      for(int i = 0; i < 1024; i++) {
        BigSizeClass c;
        ret->Add(c);
      }
      return ret;
    }
    
  2. may be I forget delete returning value:

    CArray<BigSizeClass>* test() {
      CArray<BigSizeClass>* ret = new CArray<BigSizeClass>();
      for(int i = 0; i < 1024; i++) {
        BigSizeClass c;
        ret->Add(c);
      }
      return ret;
    }
    
  3. may be I do like Expensive:

    // CArray<BigSizeClass> r = test();  --> Expensive
    // CArray<BigSizeClass>& r = test(); --> Not Expensive (Correct - because of &)
    
    CArray<BigSizeClass>& test() {
        CArray<BigSizeClass>* ret = new CArray<BigSizeClass>();
        for(int i = 0; i < 1024; i++) {
            BigSizeClass c;
            ret->Add(c);
        }
        return *ret;
    }
    
  4. may be I forget remove objects:

    CArray<BigSizeClass*> test()
    {
      CArray<BigClassSize> ret;
      for(int i = 0; i < 1024; i++)
      {
        BigSizeClass* c = new BigSizeClass;
        ret->Add(c);
      }
      return ret;
    }
    
  • 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-05-23T07:04:36+00:00Added an answer on May 23, 2026 at 7:04 am

    Way 1 is just fine. RVO and NRVO will kick in here. Even if it doesn’t you can still “swaptimize”, assuming that CArray does the smart thing and has a swap function.

    CArray<BigSizeClass> result;
    test().swap(result);
    

    However, I’d have to recommend against the use of CArray- it doesn’t use the copy constructor but instead uses memcpy, which is hideously undefined behaviour for any non-POD classes.

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

Sidebar

Related Questions

I've been searching for a good way in which to style up a selectbox,
Is there a good way to find out which exceptions a procedure/function can raise
I have a SELECT which is returning me data in the following form... ID
Which way do you prefer to create your forms in MVC? <% Html.Form() {
Which way is better for removing float decimals places or is there a more
In which way could I call the function that automatically opens my annotation (with
I am wondering which way would end up being faster, selecting elements by: $('element[href=#my_link]');
I'm really interested which way you guys use to effectivly debug a large (let's
I'm interested to find which way of creating box shadows with css is most
There are too many method for embedding flash in html, which way is the

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.