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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:01:14+00:00 2026-05-12T00:01:14+00:00

I need to check specific positions in an NSArray to see if they have

  • 0

I need to check specific positions in an NSArray to see if they have already been initialized, but I am having trouble. I tried to do the following, but it causes my application to crash!

if ((NSMutableArray *)[arrAllBlocks objectAtIndex:iLine] == nil) 
{
    [arrAllBlocks insertObject:[[NSMutableArray alloc] init] atIndex:iLine];
}

NSMutableArray *columArray = (NSMutableArray *)[arrAllBlocks
                                                objectAtIndex:iLine];
[columArray insertObject:newBlock atIndex:iColumn];

What is the best to do this? I already tried some methods like isValid, and things like that!

  • 1 1 Answer
  • 2 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-12T00:01:15+00:00Added an answer on May 12, 2026 at 12:01 am

    You have a few options here:

    Option 1: Pre-fill the array with instances of NSNull, and then use the code given by Dave DeLong in his answer.

    Option 2: (Similar to #1) pre-fill the array with instances of NSMutableArray, and then have no extra code at all. (If you’re going to pre-fill, you may as well do this).

    Option 3: Do not pre-fill the array, but insert items dynamically as required. This will be almost identical to a pre-fill if the first iLine is near the maximum:

    while([arrAllBlocks count] <= iLine)
    {
        [arrAllBlocks addObject:[NSMutableArray arrayWithCapacity:0]];
    }
    
    NSMutableArray *columArray = (NSMutableArray *)[arrAllBlocks
                                                    objectAtIndex:iLine];
    [columArray insertObject:newBlock atIndex:iColumn];
    

    Option 4: Use a dictionary to maintain the list of NSMutableArrays:

    NSString *key = [NSString stringWithFormat:@"%d", iLine];
    NSMutableArray *columnArray = [dictAllBlocks objectForKey:key];
    if (columnArray == nil)
    {
        columnArray = [NSMutableArray arrayWithCapacity:0];
        [dictAllBlocks setObject:columnArray forKey:key];
    }
    
    [columArray insertObject:newBlock atIndex:iColumn];
    

    How to choose:

    If the maximum value for iLine is not enormous, I would go with option #2. A handful of NSMutableArrays initialized to zero capacity will take up very little memory.

    If the maximum value for iLine is enormous, but you expect it to be accessed sparsely (i.e., only a few values of iLine will ever be accessed), then you should go with Option #4. This will save you from having to fill an NSMutableArray with objects that never get used. The overhead of converting the string-value key for the dictionary will be less than the overhead for creating all of those blanks.

    If you’re not sure, try out each option and profile them: measure your memory usage and the time required to execute. If neither of these options work, you may have to explore more complex solutions, but only do that if it turns out to be necessary.

    A note of caution:

    The original code that you posted has a memory leak in the following line:

    [arrAllBlocks insertObject:[[NSMutableArray alloc] init] atIndex:iLine];
    

    The NSMutableArray objects that you initialize here are never released. When you call [[NSMutableArray init] alloc], a brand new object is created (with a reference count of one). The insertObject method then adds that new object to arrAllBlocks, and retains it (increasing its retain count to 2). Later, when you release arrAllBlocks, the new array will be sent a release message, but that will only reduce its retain count to one again. At that point, it will stick around in RAM until your program exits.

    The best thing to do here is to use [NSMutableArray arrayWithCapacity:0] instead (as I have done in my examples). This returns a new NSMutableArray, just the same as your code did, but this instance has already been autoreleased. That way, arrAllBlocks can take ownership of the new object and you can be sure that it will be released when appropriate.

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

Sidebar

Related Questions

I need to check if a specific login already exists on the SQL Server,
Currently, I need to check a specific anchor tag to see if it contains
i need to check for a specific DateTime value in my table from my
In a program (Java) I'm making I need to check for a specific pin
I need to check if this index not exist in specific table name not
Need to check if DateTime is in specific range. I think I need to
I need to check if the data of a newly created OverlayItem is already
In C++/CLI code I need to check if the type is a specific generic
Possible Duplicate: Check of specific radio button is checked I have these 2 radio
I have a std::vector and I want to check a specific attribute of each

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.