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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:24:59+00:00 2026-06-08T20:24:59+00:00

Do variables declared in methods get erased after the method call is done? Ie

  • 0

Do variables declared in methods get erased after the method call is done? Ie If i have the method “someMethod” and every time it is called i want to return a different NSString from a stack property will the method return the next object on the stack or will it keep returned the first index since x is erased at the end of the method call. I know if C that variables are erased after function calls, is it the same in objective c? Would using a seperate property for the variable x some this problem? Thanks

(Double) someMethod{
int x;
[self.stack objectAtIndex:x];
x++;
}

After reading the comments I tried creating a property to replace x and here is what I wrote but I get an error warning stating “local declaration of “_location” hides instance variable” What does this mean?

@property (nonatomic) int location;
@synthesize location=_location;

-(int) location{
    if(!_location){
        int _location = 0;
     //warning is here 
    return _location;
     }
_location++;
return _location;

}

 (Double) someMethod{
int x;
[self.stack objectAtIndex:self.location];
x++;
}
  • 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-08T20:25:02+00:00Added an answer on June 8, 2026 at 8:25 pm

    Do variables declared in methods get erased after the method call is done?

    Yes

    Objective C methods are implemented “on top” of C functions, so the same rules apply. In particular, your code exhibits undefined behavior (reading of uninitialized variable).

    To fix this issue, add an instance variable x in place of the automatic variable that your code snippet currently declares.


    automatic is the “official” name of “stack” variables, i.e. variables that you declare inside your methods / functions.

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

Sidebar

Related Questions

I want to know all the variables that have been declared within an include
I remember reading that static variables declared inside methods is not thread-safe. (See What
I am trying to pass variables declared in C# to html. The variables have
I have a .h file where 2 variables are declared, then I have a
I have declared three variables namely TemplateData tData; TaskInstance tInstance;int tID; in my program.
I have a windows forms application, where I have declared some static variables. On
CoffeeScript wraps variables declared inside methods in an object literal. So, this: @Templates =
I have the 2 methods below, in a class, The Login Method works fine
I have classA that has some variables declared in its .h file (arrays, strings,
So I know that in Python variables declared in a method are not visible

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.