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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:33:04+00:00 2026-05-30T11:33:04+00:00

Should I always check if [[NSArray alloc] init…] (or with any other collection class)

  • 0

Should I always check if [[NSArray alloc] init…] (or with any other collection class) returns nil? The Apple docs say that objects may return nil if allocation or initialisation fails. I don’t know when initialising may fail with NSArray, but I guess that allocation may fail with insufficient memory. And because I’m developing for iOS, that may become a regular problem. Do I have to about that and check every time I want to create a new array, or will my app fail because of memory constraints (assuming worst-case situation, of course) and checking for nil is just a waste of cycles?

Currently, I’m only checking when I allocate a mutable collection with a large predetermined capacity (e.g. [NSMutableArray arrayWithCapacity: 1000]) or an immutable collection with lots of objects (over a thousand).

Thank you.

  • 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-30T11:33:05+00:00Added an answer on May 30, 2026 at 11:33 am

    No, not with NSArray. NSArray is a linked list using structs, so it does not malloc much behind the scenes. Checking for nil, at least with NSArray is rather pointless.

    However, if you were using a collection class like CCArray, from cocos2d, for example then checking for nil with a large array could be beneficial.

    Still, the size of a pointer on iOS is 8 bytes, and even a C-Array of 1000 elements is only 8 KB of RAM. In most cases, you won’t be using enough memory to the point where you will run out.

    Also note that if you come to the point where your application is running low on memory, there are many delegate methods that you can register to to be warned about this and fix it.

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

Sidebar

Related Questions

should always the version field be checked when updating a domain class object ?
when we want to connect to database from visual studio should we always check(before
While converting .NET DateTime (when is default(DateTime)) to SqlDateTime should I always check if
Is $_SERVER['SERVER_ADDR'] always set? Should I check with isset() or is that unnecessary? I
True or not: We should always use proper capitalization and never put whole sentences
In the below thisIsAlwaysTrue should always be true. DateTime d = DateTime.Now; bool thisIsAlwaysTrue
We generate web pages that should always be printed in landscape mode. Web browser
What are the security topics that you should always include when writing a proposal
I have previously been told that I should always use Randomize() before I use
It seems to me that one should always properly dispose resources when calling Directory

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.