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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:47:10+00:00 2026-06-10T12:47:10+00:00

cStringUsingEncoding: returns a const char * despite it is returning a dynamically allocated C

  • 0

cStringUsingEncoding: returns a “const char *” despite it is returning a dynamically allocated C string(from it’s documentation). So, what is the purpose of const here? We could simply modify the returned C string by casting to char *.

cStringUsingEncoding:

The returned C string is guaranteed to be valid only until either the
receiver is freed, or until the current autorelease pool is emptied,
whichever occurs first.

I think library is following the common practice of pointer-to-const; it’s not expected to be modified or released.

From Objective-C runtime;

const char * object_getClassName(id obj) — Nothing specified about the returned string.

char * method_copyArgumentType(Method method, unsigned int index) — You must free the string with free(). (May be it’s advising because of it’s returning a copy.)

  • 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-10T12:47:11+00:00Added an answer on June 10, 2026 at 12:47 pm

    The common pattern is that you should not modify buffers that you don’t own. const documents and (somewhat) enforces this.

    As for cStringUsingEncoding:, The documentation is saying that the returned buffer is only valid as long as the NSString from which you received it, or for the duration of the current autorelease pool. This implies that you do not own the returned buffer, because you’re not expected to release it.

    Your last two examples from the runtime follow the same convention:

    • const char * object_getClassName(id obj)

      Doesn’t inform you that you should release the buffer, and the name doesn’t contain any indication that you own the buffer. Therefore you don’t free() it, and you don’t modify it.

    • char * method_copyArgumentType(Method method, unsigned int index)

      The docs explicitly tell you that you should free the buffer, and the function name contains the tell-tale copy which also implies that you own the buffer. Therefore you can modify it all you want, and must free() it.

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

Sidebar

Related Questions

NSString * string = @االْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ; const char *c = [string cStringUsingEncoding:NSUTF8StringEncoding];
Please consider the following code: NSString *string = @ä; const char *str1 = [string
I need to build up a const char string of other const char 's
Suppose I'm getting a C string from some function: char * mystring = SomeCFunction(...);
Hey, I am trying to convert NSString to a C string using cStringUsingEncoding but
I want to convert a NSString into a const char * in order to
I am using following code to convert string to sha1 string but i am
I have implemented a client-server transferring from Windows desktop application to iPhone App. I
I am trying to draw a string using quartz 2d. What i am doing
I have a class called options written in c++, here is the header info:

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.