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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:48:57+00:00 2026-06-14T11:48:57+00:00

This is probably a total noob question, but I just can’t seem to figure

  • 0

This is probably a total noob question, but I just can’t seem to figure this out by myself. I searched here and google, but didn’t had any luck.

I have a class with some name, for instance, ‘logme’. I implement a float *floaters in the .h file, make a property en synthesize it in the .m file. Two methods inside the .m file method1 and method2. When I perform method1 from the viewcontroller, it makes the floaters variable an array with some values. I NSLog them to check if they are and they are. After that I perform method2 which only NSLogs the same array but now the values in the array are totally different.

logme.h

@interface Logme : NSObject {
    float *floaters;
}
-(void)method1;
-(void)method2;

logme.m

@implementation Logme
-(void)method1 {
    floaters = (float[8]) {
        1.0f, 2.0f,
        3.0f, 4.0f,
        6.0f, 5.0f,
        7.0f, 8.0f,
    };
    for (int i = 0; i < 8; i++) {
        NSLog(@"%f", floaters[i]);
    }
}
-(void)method2 {
    for (int i = 0; i < 8; i++) {
        NSLog(@"%f", floaters[i]);
    }
}
@end

Calling the methods from viewcontroller:

logme = [Logme alloc];
[logme method1];
[logme method2];

Outcome:

NSLog from method1:
1.000000
2.000000
3.000000
4.000000
6.000000
5.000000
7.000000
8.000000

NSLog from method2:
0.000000
-1.998885
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
  • I’m sorry. I was fighting this 4 spaces system. It wouldn’t let me add the .m file code. Finally got it in here 🙂
  • 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-14T11:48:59+00:00Added an answer on June 14, 2026 at 11:48 am

    Your are assigning a pointer to a block of memory that is in the stack, this means that when you exit from mrthod1 that memory will be deallocated.The value printed after method1 are casual, it may also happen that the memory gets not touched, but usually the stack changes at every method call, so that’s your case.

    Choice 1

    You are using floaters as an array, so declare it to be an array and it will have it’s own storage:

    @interface Logme : NSObject {
    float floaters[8];
    }
    
    -(void)method1 {
        for(int i=0; i<8;i++)
            floaters[i]=i+1;
    for (int i = 0; i < 8; i++) {
        NSLog(@"%f", floaters[i]);
    }
    

    Choice 2

    Let floaters be a pointer, but allocate dinamically the memory for floaters:

    -(void)method1 {
    floaters=(float*)malloc(8*sizeof(float));
    for(int i=0; i<8; i++)
        floaters[i]=i+1;
    for (int i = 0; i < 8; i++) {
        NSLog(@"%f", floaters[i]);
    }
    

    And don’t forget to free the memory when you don’t need it anymore.

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

Sidebar

Related Questions

This is probably a stupid question but I can not seem to either remember
This question probably reveals my total lack of knowledge in Java. But let me
This is probably the dumbest question ever here on stack Overflow. But I am
This is probably a total newb question but does anyone know how to make
Total Python noob here, probably missing something obvious. I've searched everywhere and haven't found
This probably sounds like a terrible idea at first glance, but here is my
This probably is a dummy question but I cannot find a clear indication. I
Im sorry for this probably dumm question, but I want to simply open modals
This is probably a simple question, but it's driving me crazy! I have a
There is probably a reeeeeally obvious answer to this, but I can't find it,

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.