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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:18:26+00:00 2026-05-17T17:18:26+00:00

I want to convert a NSString into a const char * in order to

  • 0

I want to convert a NSString into a const char * in order to access a sqlite DB.

This works:

NSString *queryStatementNS = @"select title from article limit 10";
const char *queryStatement = [queryStatementNS UTF8String];

This causes a crash in the simulator (without any stacktrace):

NSString *queryStatementNS = [NSString stringWithFormat:@"select title from article limit %d", 10];
const char *queryStatement = [queryStatementNS UTF8String];

Can anybody tell me, what the stringWithFormat method changes in the String to make the conversion to UTF8 (or to ASCII using cStringUsingEncoding:NSASCIIStringEncoding) crash? The same crash happens also when passing no arg at all to the stringWithFormat. Could it be related to memory management somehow?

  • 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-17T17:18:26+00:00Added an answer on May 17, 2026 at 5:18 pm

    From the documentation:

    The returned C string is automatically
    freed just as a returned object would
    be released; you should copy the C
    string if it needs to store it outside
    of the autorelease context in which
    the C string is created.

    Your problem is that queryStatement is being freed when queryStatementNS gets deallocated, and as queryStatementNS is autoreleased you don’t know exactly when this is going to occur. You can either retain queryStatementNS by calling

    [queryStatementNS retain]
    

    at some point in that function (remember to release it when you want to relinquish ownership), you can explicitly create a non-autoreleased string to deal with yourself by saying

    NSString* query = [[NSString alloc] initWithFormat:@"a string! %d", 10, nil]
    

    (as an aside, note the nil – if you don’t have it there xcode will give you a missing sentinel warning)

    or you can copy the output of [queryStatementNS UTF8String] to your const char* queryStatement as you would in plain C, with strcopy or whatever.

    The reason the first example you give continues to work is that you’re setting a pointer to a string literal, @”select title from article limit 10″. The objective c compiler ensures that there’s only ever one instance of this string in memory, no matter how many times you reference it in your code. Thus, it doesn’t obey the standard memory management conventions of objective c and your pointer remains valid outside of the autoreleased context.

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

Sidebar

Related Questions

I want to convert from char representing a hexadecimal value (in upper or lower
I have taken char data into database into array. now i want to convert
I have an NSString like @Hello World and want to convert that into an
I want to convert a primitive to a string, and I tried: myInt.toString(); This
I want to convert a string into a double and after doing some math
I want to convert a string into a series of Keycodes, so that I
I have an RGB hex code like #ffffff as NSString and want to convert
in my project i want to convert NSString value to NSdata without encoding, i.e
I have a need to convert NSString to double. If this string is in
I want to convert an instance of generic IDictionary<,> to non generic IDictionary .

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.