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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:25:31+00:00 2026-05-22T12:25:31+00:00

Do [[NSMutableArray alloc] init]; and [[NSMutableArray alloc] initWithCapacity:0]; compile into the exact same thing?

  • 0

Do

[[NSMutableArray alloc] init];

and

[[NSMutableArray alloc] initWithCapacity:0];

compile into the exact same thing?

If they differ, then how, and which form is “better” in terms of memory and runtime performance?

  • 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-22T12:25:32+00:00Added an answer on May 22, 2026 at 12:25 pm

    No, they will not behave identically. init will create an array with some unknown but most likely nonzero capacity—whatever the authors decided was a reasonable default for most situations. initWithCapacity:0 will request an array with absolutely no allocated space. What this means is up to the NSMutableArray implementation: it might not allow a capacity of 0 and behave exactly as init, or it might not immediately allocate anything and instead allocate some amount (the same as init, possibly, or maybe not) when you first need it.

    Which will perform “better” completely depends on how you expect to use the array. -init says “I have no expectations for this array; I’ll either be adding to and removing from it a lot, or it will likely be pretty small.” -initWithCapacity: says “I expect this array to have no more than this many elements for the foreseeable future.”

    About the only place where I would expect initWithCapacity:0 to be reasonable is if you are creating an array that you don’t expect to fill with anything for a fairly long period of time.

    Note that the standard performance caveat applies here: it’s probably not an issue unless you profile and determine that it is. I can’t really imagine a situation (except for thousands upon thousands of NSMutableArray objects) where the difference between these two will be substantial.

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

Sidebar

Related Questions

NSMutableArray *a1 = [[NSMutableArray alloc] init]; NSMutableArray *a2 = [NSMutableArray array]; TempObj *obj =
Heres my array NSMutableArray *arrayTmp= [[NSMutableArray alloc] init]; I populate it like this double
I'm looking at this code: NSMutableArray *controllers = [[NSMutableArray alloc] init]; for (unsigned i
here is what i am trying to do: NSMutableArray *objectNames = [[NSMutableArray alloc] init];
Hi I want to put this into a function. NSMutableArray* weekArray = [[NSMutableArray alloc]
-(IBAction)Done :(id) sender { NSMutableArray *addinfo = [[NSMutableArray alloc]init]; AddinfoCarteV *addinfoCarteV =[[AddinfoCarteV alloc]initWithNibName:@AddinfoCarteV bundle:nil];
Car class -------------- price color crash code is: NSMutableArray *list = [[NSMutableArray alloc] init];
I have a simple code: NSMutableArray *arrayCheckList = [[NSMutableArray alloc] init]; [arrayCheckList addObject:[NSMutableDictionary dictionaryWithObjects:[NSArray
Ok so here's what I'm doing. NSMutableArray *array = [[NSMutableArray alloc] init]; UIView *tempview
I have a class that subclasses NSMutableArray. I init it using: MyClass class =

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.