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

  • Home
  • SEARCH
  • 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 445795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:24:24+00:00 2026-05-12T21:24:24+00:00

I have two functions which one should i use? Please explain the difference. A:

  • 0

I have two functions which one should i use?
Please explain the difference.

A:

- (NSMutableArray *)FunctionA:(int)count {

    NSMutableArray *a = [[NSMutableArray alloc] init];

 for (int i = 0; i < count; i++) {
  [a addObject:[NSNumber numberWithInt:0] ];
    }

    return [a autorelease];
}

B:

-(NSMutableArray *)FunctionB:(int)count {

 NSMutableArray *b = [NSMutableArray arrayWithCapacity:count];

 for (int i=0;i<count; i++){  
  [b addObject:[NSNumber numberWithInt:0] ];  
 }

 return b;  //  or [b autorelease] ?
}
  • 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-12T21:24:25+00:00Added an answer on May 12, 2026 at 9:24 pm

    The first creates a mutable array without specifying a capacity. This causes the array to have to grow when you add items. Internally, this is probably heavily optimized to occur “chunks at a time” but it’s still necessary to grow the array and allocate more space when adding items.

    The second gives the array a hint (you’re probably going to need “this much” room) to avoid the overhead of growing the array when adding a known number of objects. Of course it will still grow larger if needed (as if you hadn’t specified a capacity). You should use this approach if you already know the count ahead of time. It’s faster with a large count.

    This has a downside if you haven’t measured before optimizing, however: If you’re creating a mutable array with a very high capacity but not always using that capacity, you incur the penalty of allocating all that space for nothing.

    Also, you don’t autorelease B (as you commented out) because you didn’t create the mutable array with init – you used a convenience method which did it itself, which means you’re not responsible for releasing it. As I mentioned in a comment to another answer to your question, you can also create the array with:

    [[NSMutableArray alloc] initWithCapacity:capacity];

    … then release it when ready. This gives you greater control over memory usage than using the autorelease pool, which is an important consideration on the iPhone platform.

    Remember, though: measure first, then optimize if necessary.

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

Sidebar

Related Questions

Hi I have a program written in C++ in which one or two functions
i have two data classes which hold only data members(no functions). One is CallTask
Suppose I have two functions which look like this: public static void myFunction1(int a,
I have a function which returns a one-sided intersection of values between two input
I have a function which takes in two parameters, and returns one or the
I have two Python functions, both of which take variable arguments in their function
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
I have a function which takes two arguments, the ID of an item (wine)
I have to make a function which concatenates two strings but I have to
I have a merge function which takes time O(log n) to combine two trees

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.