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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:31:04+00:00 2026-05-23T13:31:04+00:00

Assume following code, NSString *str=[[NSString alloc] initWithString:@sagar]; [str autorelease]; I have seen many times

  • 0

Assume following code,

NSString *str=[[NSString alloc] initWithString:@"sagar"];


[str autorelease];

I have seen many times that, Most of the programmers do prefer to put alloc, init simultaneously within one statement.

Here, I am asking for the possibility of dividing autorelease for next statement.

  • For example, It is recommended to put init with alloc statement.
    Is it same for 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-23T13:31:05+00:00Added an answer on May 23, 2026 at 1:31 pm

    Most of the programmers do prefer to put alloc, init simultaneously within one statement.

    That’s because the instance returned by the initialiser is not necessarily the one returned by +alloc. For example, this is wrong and will crash your program:

    NSString *str = [NSString alloc];
    [str initWithString:@"sagar"];
    

    because in this case -initWithString: causes the deallocation of the previous instance, and str ends up pointing to a deallocated object. This can be fixed by:

    NSString *str = [NSString alloc];
    str = [str initWithString:@"sagar"];
    

    so that str points to the different instance returned by -initWithString:. The form:

    NSString *str = [[NSString alloc] initWithString:@"sagar"];
    

    guarantees that str points to the correct instance.


    That said, -autorelease is different. Unless it’s been overridden by an evil djinn, it always returns the receiver itself. This means that both:

    NSString *str = [[NSString alloc] initWithString:@"sagar"];
    str = [str autorelease];
    

    and:

    NSString *str = [[NSString alloc] initWithString:@"sagar"];
    [str autorelease];
    

    are correct and work in the same manner.

    As for the distinction between:

    NSString *str = [[[NSString alloc] initWithString:@"sagar"] autorelease];
    

    and:

    NSString *str = [[NSString alloc] initWithString:@"sagar"];
    …
    [str autorelease];
    

    some people prefer to use -autorelease in tandem with allocation to avoid forgetting to autorelease the instance later. Others prefer to place it in the return statement (if any):

    NSString *str = [[NSString alloc] initWithString:@"sagar"];
    …
    return [str autorelease];
    

    to make (more) explicit that the method/function returns an autoreleased object.

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

Sidebar

Related Questions

I have the following code: NSString *subtitle = [[[node elementsForName:@subtitle] objectAtIndex:0] stringValue]; NSString *duration
I'm stuck with the following bit of code. NSString *gridRef = [[NSString alloc] initWithFormat:
Assume the following code, without any ref keyword, that obviously won't replace the variable
Assume I have the following code: public static class Foo { public static void
I have the following code that responds to a button click, changes the view
let's assume we have the following code: public class TestScope { private int a
Assume the following code: using (SqlConnection conn = new SqlConnection(connectionString)) { ... using (SqlCommand
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
When writing GUIs, I've frequently come over the following problem: Assume you have a
Does anyone know if the following code could be problematic: NSString *addchar = nil;

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.