Re: sprintf and 64-bit integers
Re: sprintf and 64-bit integers
- Subject: Re: sprintf and 64-bit integers
- From: Steve Christensen <email@hidden>
- Date: Sun, 13 Sep 2009 11:15:39 -0700
On Sep 13, 2009, at 11:10 AM, email@hidden wrote:
On Sun, Sep 13, 2009 at 8:01 PM, Bill Bumgarner <email@hidden> wrote:
On Sep 13, 2009, at 10:59 AM, email@hidden wrote:
I'm updating my code for Snow Leopard and ran into this problem. The
app crashes at this line:
sprintf(str, "%d", val);
where val is a CFIndex. According to the string programming guide
here...
http://developer.apple.com/mac/library/documentation/Cocoa/
Conceptual/Strings/Articles/formatSpecifiers.html
...I need to cast my CFIndex to long and replace the %d format
specifier to %ld. I tried that but still got the crash. So I kept
trying with all the format specifiers in the book, declaring my
variable a NSInteger, unsigned int, etc, but no matter what the app
kept crashing. The only that that actually worked was %lx, but
then I
get the numbers all wrong. It seems that sprintf only accepts 32-bit
integers. Is this correct? If so, what's the workaround? I'm
compiling
for both 10.5 and 10.6. Advice appreciated, thanks.
You are off in the weeds.
There is nothing about a value conversion that could cause a
crash. Wrong
value? Sure. But not a crash. Thus, the formatting string is
*not* causing
a crash.
The problem is almost assuredly that 'str' is pointing to garbage,
uninitialized or otherwise wrong.
Post the code for how str is created.
b.bum
Sorry, str is simply created like this:
char str[10];
sprintf(str, "%d", val);
For a 64-bit unsigned integer, the maximum decimal value is
18446744073709551615. A quick count shows that to be 20 characters
long, not including the null-terminator. Stuffing 20 characters into
a local buffer is likely to trash the stack frame.
steve
_______________________________________________
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