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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:44:25+00:00 2026-05-23T20:44:25+00:00

I have been programming in Objective-C for a couple weeks now, and I think

  • 0

I have been programming in Objective-C for a couple weeks now, and I think its time that I think more about my memory management and so I have a couple scenarios I would like clarification on.

NSString *myString = @"Hello, World";

If I were to on the next line do

NSString *anotherString = myString;

Is it getting copied? or is another string just a pointer to myString? if it’s just a pointer how would I make anotherString a copy of myString that has its own memory?

What if I were to

[anotherString release];

What actually gets released, just anotherString, or does myString go away too?

Then if I were to do

anotherString = nil;

Does that actually free up memory and for use or does it still have the space allocated with empty memory? Obviously the pointer can still be used. What does it do to myString?

This will really help me become better at managing the memory properly, instead of just [object release]; randomly and causing crashes.

Thank you VERY MUCH in advance to whoever replies.

  • 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-23T20:44:26+00:00Added an answer on May 23, 2026 at 8:44 pm

    When you do:

    NSString *myString = @"Hello, World";
    NSString *anotherString = myString;
    

    …you’re creating a single NSString instance, and two pointers to it. So to answer your questions, no it is not being copied. Yes, anotherString is just a pointer, and so is myString, the actual NSString instance is off in memory-land, at the address that both pointers are now pointing to.

    To make a copy of the string, you would do:

    NSString *anotherString = [myString copy];
    

    When you do:

    [anotherString release];
    

    The NSString instance that anotherString points to is released. Nothing happens to anotherString itself. It continues to point to your now-invalidated object instance. If myString was pointing to the same instance, then yes, this causes myString to “go away” as well.

    And if you do:

    anotherString = nil;
    

    …then you’ve caused a memory leak, because you reassigned the pointer without releasing the thing it was pointing at. Except not in this case because @"Hello, World" is a literal value that is loaded onto the stack and is not retained by your code.

    In any case, setting anotherString to nil does nothing to your string, it just changes what anotherString points to. Your actual NSString instance is still off in memory-land, completely unaware that you just threw away your reference to it (possible exception: reference counted/garbage collected environments).

    And yes, calling [object release] randomly will cause crashes. As a general rule you should only call release on something if:

    1. You called alloc and init... on it.
    2. You called retain on it.
    3. You obtained the object as a result of calling copy on some other object.

    Note that these stack, so if you alloc something, retain it, and then copy it, you need to have 3 calls to release, two for the object that you alloc‘ed, and one for the copy.

    As a side note, you may find it clearer to write your object declarations like:

    NSString* myString = @"Hello, World";
    NSString* anotherString = myString;
    

    …which makes it more obvious that what you are declaring are pointers to NSString’s and not NSString’s themselves. Personally I think your original syntax, while commonly used, is also backwards.

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

Sidebar

Related Questions

I have been reading about bit operators in Objective-C in Kochan's book, Programming in
I've been programming objective-C for a few months now and have done pretty well
I have been programming in Perl, off and on, for years now, although only
I have been programming in Python, PHP, Java and C for a couple or
At school we have been programming in MIPS assembly language for some time. I'm
I have been developing on the iPhone with Objective-C for a few months now
After spending three weeks learning Objective-C and Cocoa programming for my work, I've been
I have been programming in C/C++ for about five years and Python for three;
I have been programming for the last 8 years and now I was just
I just started studying programming about 6 months ago and I have really been

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.