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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:39:27+00:00 2026-06-04T15:39:27+00:00

I have the following C array of NSString * : static NSString *const OrderByValueNames[]

  • 0

I have the following C array of NSString *:

static NSString *const OrderByValueNames[] = {@"None",@"Added",@"Views",@"Rating",@"ABC",@"Meta"};

Now, I want to check the length of this array at runtime so I wrote the following method:

NSInteger LengthOfArray(NSString *const array[])
{
    NSInteger length = 0;
    // For Loop Without a body!
    for (length = 0; array[length] != nil; length++);
    return length;
}

Now, when I run this code at debug configuration, everything is fine and the function returns the right result.

BUT as soon as I switch to release configuration and run it again, the program freezes at the for loop. After 10 seconds of the loop being executed, iOS kills the app for not responding. Weird.

Now, if I add body to the loop, like that:

for (length = 0; array[length] != nil; length++)
{
    NSLog(@"%d",length);
}

Then it’s working fine even in release mode.

Giving the loop an empty body, like that:

for (length = 0; array[length] != nil; length++){}

Still freezes in release mode.

My guess is that there is a compiler optimization when running in release mode, but what exactly?!

Any ideas?

  • 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-06-04T15:39:29+00:00Added an answer on June 4, 2026 at 3:39 pm

    C-style arrays are not nil-terminated by default. If you want to check the length this way, you need to add the terminator yourself.

    static NSString *const OrderByValueNames[] =
       {@"None",@"Added",@"Views",@"Rating",@"ABC",@"Meta",nil};
    

    A much better way to find the length is simply this:

    length = sizeof(OrderByValueNames) / sizeof(OrderByValueNames[0]);
    

    (Note that this trick doesn’t work if you pass the array into a function, since it then degenerates into a pointer.)

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

Sidebar

Related Questions

I have following array and search string. NSArray *values =[NSArray arrayWithObjects:@abc,@xyz,@cba,@yzx,nil]; NSString *search =
I have following array, that I need to operate by hand on bitmaps. const
I have the following array, $myarray[0]['first_name'] $myarray[0]['last_name'] I want to turn it into: $myarray['first_name']
I have the following array, I need to reorder it by average rating descending.
I have the following array: NSString *testString = @1|Car|Red|Ford; NSArray *testArray = [testString componentsSeparatedByString:@|];
I have the following code: NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; NSString *temp;
I have successfully created video from images using the following code -(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path
I have the following array. <cfset ItemHasUsers = arrayNew(1)> <cfloop query=qReadData> <cfset ItemHasUsers[qReadData.currentrow][ID] =
I have the following array as3 example: var arrayDefinitionsargsAmfPhp:Array = new Array(); arrayDefinitionsargsAmfPhp['tabela'] =
I have the following array: a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] I use it for some visual

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.