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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:18:16+00:00 2026-06-13T05:18:16+00:00

I am looking for some clarification on initializing variables in Objective C. Say I

  • 0

I am looking for some clarification on initializing variables in Objective C.

Say I have a method that returns an array.

-(NSMutableArray *) getArray
{
    NSMutableArray *arr = [[NSMutableArray alloc]init];  //line A

for(int i = 0; i < 10; i++)
{
    [arr addObject:@"word"];
}

return arr;
}

And then I call this method.

NSMutableArray *myArray = [[NSMutableArray alloc]init];  //line B
                myArray = [self getArray];

So should I be allocating memory in both lines A and B, neither, or in just A or just B? The alternative being simply

NSMutableArray *arr;      //replacing line A
NSMutableArray *myArray;  //replacing line B
  • 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-13T05:18:17+00:00Added an answer on June 13, 2026 at 5:18 am

    For any one array, you should be allocating its memory and initializing it once.

    To start with, that means that your alternative at the end doesn’t work. It’s declaring that these two variables exist and will point to arrays but does nothing to create and assign them.

    Line B creates and initializes an array just fine, but then immediately loses its only reference to it by assigning the result of getArray to the same variable. If you use ARC memory management, that’s a bit wasteful; without ARC, it’s a memory leak.

    Line A also creates and initializes an array correctly and, as far as the code you’ve posted goes, that’s the one that gets affected by whatever you do next to myArray.

    (Remember that the things you declare as variables — like NSMutableArray *arr — can be thought of as names for the actual objects rather than objects themselves.)

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

Sidebar

Related Questions

I'm looking for some clarification regarding the behaviour of redirect_to . I have this
I have script that gets some data using DBI's fetchall_hashref(). Usually it returns a
Just looking for some clarification with the new play button. I notice that when
Looking for some guidance on a WCF service I’m prototyping. I have a WCF
looking for some help with images referenced within the stylesheet. I have no problems
So I'm looking for some clarification how it would be possible to remove the
Hi guys looking for some guidance, i have a knowledge of xhtml, css, javascript,
I'm looking on the datatables.net website for some clarification or documentation rather about what
Need a clarification while looking at calculating a running checksum. Assume I have data
I have some divs that when the page loads are populated with images of

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.