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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:46:08+00:00 2026-05-26T18:46:08+00:00

I am somewhat confused about memory allocation of static variable in objective C. should

  • 0

I am somewhat confused about memory allocation of static variable in objective C.

  1. should we allocate memory for static variable using alloc:? and
    initialize it usinginit:?

  2. Is objective c static variable same as c static variable?

  3. is it worth to apply retain on static variable?

  • 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-26T18:46:09+00:00Added an answer on May 26, 2026 at 6:46 pm

    There are a couple of things you need to take into account.

    First, static C-like variables inside functions are technically fine. This should be:

    - (void) f
    {
        static NSString* s = nil;
        if (s == nil) {
            s = [[NSString alloc] initWithString:@"Hello"];
        }
    }
    

    The problem is that you will probably never be able to release, since s scope is only f function.

    This is exactly like a C static variable inside a function.

    There is also static usage to control visibility of a variable in a translation unit. So, if you want to only allow some variable to be accessed within file.c, just like C, you need to write in that file:

    static NSString *s = [[NSString alloc] initWithString:@"Hello"];
    

    Again, unless you write a specific method to release when you application ends, it will probably leak memory.

    Finally, you can have “object oriented like” static behavior. You don’t use static keyword here but as most object oriented languages, you can have class variables (in Java, C#, C++ and others, class variables are accomplished with the static keyword).

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

Sidebar

Related Questions

I am learning about SOAP implementation and have become somewhat confused regarding the appropriate
I'm somewhat confused about the lifecycle of ManagedBeans of type request. In this example
I somewhat confused because I've read that everything should be possible at IRQL_PASSIVE, but
I am somewhat confused about how the group by command works in mysql. Suppose
I am somewhat confused about when things are allocated on the heap (and I
I've come across the page in SQL Server 2008, and is somewhat confused about
I am somewhat confused about presence of two seemingly identical VB.NET functions: CType(args) and
i am studying about views in sql and somewhat confused and having the following
I am somewhat confused about a paragraph in the code complete book. In the
somewhat confused.. but trying to do a search/repace using wildcards if i have something

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.