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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:03:32+00:00 2026-05-25T14:03:32+00:00

I have 2 new questions: 1) Consider this line: NSString *myString = [[NSString alloc]

  • 0

I have 2 new questions:

1) Consider this line:

NSString *myString = [[NSString alloc] initWithString: @"Value"]; 

There were two things I learned, but I would like confirmation:
As I learned, the “alloc” message indicates that the instance of NSString will be stored in the “heap” memory.
I understood also that primitive variables such as “chars” are stored in the “stack” memory.

Does this mean that:

  • the instance of NSString is stored in the heap memory;
  • AND that this object has an iVar pointer (when the initWithString method was called) to the “Value” string of primitive “chars”, which reside in the stack memory?
    How does this work in actuality?

The second question is directly related and causes for me a personal dilemma (probably because I’m missing a point):
2) Which of the two approaches would you consult and why?:

NSString *myString = [[NSString alloc] initWithString: @"Value"]; 
NSString *myString = @"Value"; 

If my first question is confirmed, both approaches should “in the end” point to chars that are stored in the stack memory. I therefore don’t actually see the purpose of using the first option and being bothered with the retain count.

  • 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-25T14:03:33+00:00Added an answer on May 25, 2026 at 2:03 pm

    Short answer: In this case, both lines have the same result It’s fine to assign the string constant directly to myString.

    Longer answer:

    It’s true that Objective-C objects are allocated on the heap. It’s not true, though, that “primitive” values are always stored on the stack. Local variables are stored on the stack, whether they’re primitive or not. (You can, for example, declare a local variable that’s a struct, which isn’t considered primitive.) You can store primitive values on the heap, but the only way to access them in that case is via a pointer. For example:

    int *someInt = malloc(sizeof(int));  // allocate a block of memory to hold an int
    *someInt = 42;                       // store data in that memory
    

    The reason that we always use pointers to refer to Objective-C objects is that objects are always allocated on the heap. If you want to store something on the stack, the compiler needs to know its size. In Objective-C, the size of an object isn’t known until the program is actually running.

    So, back to your strings. Try executing the following two lines:

    NSString *foo = [[NSString alloc] initWithString:@"foo"];
    NSString *bar = @"foo";
    

    If you break after the second line, you’ll find that foo and bar contain the same address; that is, they point to the same object. Because NSString objects are immutable, creating one with a constant string just returns a pointer to that constant.

    Why is there even an -initWithString: in NSString if that’s all it does? NSString is a “class cluster,” which is to say that NSString is the public interface to several different internal classes. If you pass a NSString* that’s not a constant into -initWithString:, the object you get back might be an instance of a different class than what you get when you use the constant. As a class cluster, NSString hides a lot of implementation details from you so that you get good performance for different types of strings without having to worry about how it all works.

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

Sidebar

Related Questions

still new to XML parsing with the iphone so i have a few questions.
I am new to JNI. And have a few questions : Can JNI handle
I'm very new to TDD world. I have a few questions regarding TDD. Do
I'm brand new to SQL Server 2008, and have some newbie questions about the
I am relatively new to world of programming. I have a few performance questions:
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
Consider this example Javascript code: a = new Array(); a['a1']='foo'; a['a2']='bar'; b = new
I have several questions about best use of exceptions in Java. Consider the code
I'm new in using lambda expression sorry for a dumb question. Anyway consider this
i have trouble with building some projects. please consider this scenario: i have 2

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.