• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Can an NSArray ever have a count of -1?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can an NSArray ever have a count of -1?


  • Subject: Re: Can an NSArray ever have a count of -1?
  • From: Jens Alfke <email@hidden>
  • Date: Fri, 19 Feb 2016 13:29:45 -0800

> On Feb 19, 2016, at 1:17 PM, Jim Adams <email@hidden> wrote:
>
>            SLogInfo(@"Starting csi %ld count %d", csi, sortedEvents.count);
>
> In the console I see:
> INFO: Starting csi -1 count -1
> The very next line crashes when the sortedEvents are accessed. What could cause the array to have a -1 count?

You’re using the wrong format strings for both of those parameters.

csi is 64-bit so you need %lld. The formatter sees %ld and thinks it’s 32-bit not 64-bit, so it skips the wrong amount of space on the stack when going to the next parameter, which is why you then get a bogus value for the count.

Also, NSArray.count is of type NSUInteger, which is unsigned, and has different sizes on different platforms. The right format specifier is either %lu (in a 32-bit app) or %llu (in 64-bit).

FYI, You must be ignoring compiler warnings, or you've turned off the “type check printf” warning setting. Don’t do that ;) I always strongly recommend turning on “Treat warnings as errors”, which will force you to fix warnings. This is very useful because a number of compiler warnings are actually nasty and hard-to-find errors like the above.

—Jens

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Can an NSArray ever have a count of -1?
      • From: Jean-Daniel Dupas <email@hidden>
References: 
 >Can an NSArray ever have a count of -1? (From: Jim Adams <email@hidden>)

  • Prev by Date: Can an NSArray ever have a count of -1?
  • Next by Date: Re: Can an NSArray ever have a count of -1?
  • Previous by thread: Can an NSArray ever have a count of -1?
  • Next by thread: Re: Can an NSArray ever have a count of -1?
  • Index(es):
    • Date
    • Thread