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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:19:06+00:00 2026-06-15T13:19:06+00:00

Basically I have a class method that returns a float array . If I

  • 0

Basically I have a class method that returns a float array. If I return a static array I have the problem of it being too large or possibly even too small depending on the input parameter as the size of the array needed depends on the input size. If I return just a float array[arraysize] I have the size problem solved but I have other problems.

Say for example I address each element of the non-static float array individually e.g.

    NSLog(@"array[0] %f array[1] %f array[2] %f",array[0],array[1],array[2]);

It prints the correct values for the array. However if I instead use a loop e.g.

    for (int i = 0; i < 3; i++) 
 {
    NSLog(@"array[%i] %f",i,array[i]);
 }

I get some very strange numbers (apart from the last index, oddly). Why do these two things produce different results? I’m aware that its bad practice to simply return a non static float, but even so, these two means of addressing the array look the same to me.

Relevant code from class method (for non-static version)…

    float array[arraysize];

    //many lines of code later

    if (weShouldStoreValue == true) {
         array[index] = theFloat;
         index = index + 1;
     }

    //more lines of code later

    return array;

Note that it returns a (float*).

  • 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-15T13:19:08+00:00Added an answer on June 15, 2026 at 1:19 pm

    You can’t do that, array dies when you exit from the method or function scope.
    You can do this using malloc in plain C:

    float* array= malloc(arraysize*sizeof(float));
    
    //many lines of code later
    
    if (weShouldStoreValue == true) {
         array[index] = theFloat;
         index = index + 1;
     }
    
    //more lines of code later
    
    return array;
    

    Don’t forget to free the memory associated.
    If you got habit to use the garbage collector and to con’t care about the memory, wrap the malloc’s data into a NSData object so that it will be freed, allocating with a method like this one:

    + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)freeWhenDone;
    // Pass YES to the freeWhenDone argument
    

    But at this point why not using a NSMutableArray of numbers?

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

Sidebar

Related Questions

So basically in my UserController.cs class I have an Index method that returns the
I have a static methods class, Utils, that is basically for utility methods, its
I basically have a class that has a factory method that takes in a
I have a method that returns an instance of a private class and I
I have a Block class that basically contains an array of 36 vertices, a
I have written a custom class for overriding the seek method of QSlider. Basically
I have a class that basically stores files in amazon s3. Here is what
I have a class that is its own activity that basically i use to
I'm testing a C++ class with a number of functions that all have basically
I have a large static list which is basically a lookup table, so I

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.