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

  • Home
  • SEARCH
  • 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 6781533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:38:06+00:00 2026-05-26T16:38:06+00:00

What is the difference between these two lines? NSString * string = @My String;

  • 0

What is the difference between these two lines?

NSString * string = @"My String";
NSString * string = [[[NSString alloc] initWithString:@"MyString"] autorelease]
  • 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-26T16:38:07+00:00Added an answer on May 26, 2026 at 4:38 pm

    @”My String” is a literal string compiled into the binary. When loaded, it has a place in memory. The first line declares a variable that points to that point in memory.

    From the string programming guide:

    The simplest way to create a string object in source code is to use
    the Objective-C @”…” construct:

    NSString *temp = @"/tmp/scratch"; 
    

    Note that, when creating a string
    constant in this fashion, you should avoid using anything but 7-bit
    ASCII characters. Such an object is created at compile time and exists
    throughout your program’s execution. The compiler makes such object
    constants unique on a per-module basis, and they’re never deallocated,
    though you can retain and release them as you do any other object.

    The second line allocates a string by taking that literal string. Note that both @”My String” literal strings are the same. To prove this:

    NSString *str = @"My String";
    NSLog(@"%@ (%p)", str, str);
    
    NSString *str2 = [[NSString alloc] initWithString:@"My String"];
    NSLog(@"%@ (%p)", str2, str2);
    
    NSString *copy = [str2 stringByAppendingString:@"2"];
    NSLog(@"%@ (%p)", copy, copy);
    

    Outputs the same memory address:

    2011-11-07 07:11:26.172 Craplet[5433:707] My String (0x100002268)
    2011-11-07 07:11:26.174 Craplet[5433:707] My String (0x100002268)
    2011-11-07 07:11:26.174 Craplet[5433:707] My String2 (0x1003002a0)
    

    What’s telling is not only are the first two string the same memory address, but if you don’t change the code, it’s the same memory address every time you run it. It’s the same binary offset in memory. But, not only is the copy different but it’s different every time you run it since it’s allocated on the heap.

    The autorelease has no affect according to the doc ref above. You can release them but they are never deallocated. So, they are equal not because both are autoreleased string but that they’re both constants and the release is ignored.

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

Sidebar

Related Questions

In C#, what is the difference (if any) between these two lines of code?
Is there a difference between these two lines? var url = http://www.google.com/; window.location =
Can you please explain me exact difference between these two lines? NSArray *foo =
Suppose I have a socket. What is the difference between these two lines of
I was wondering what is the difference between these two lines of code? int
In particular, is there any difference between these two lines in terms of their
What is the difference between these two lines of code? int *ptr = &x;
I'm trying to figure out the difference between these two lines of code.. We
What is actually the difference between these two casts? SomeClass sc = (SomeClass)SomeObject; SomeClass
What is the difference between these two pieces of code type IInterface1 = interface

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.