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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:35:45+00:00 2026-05-23T23:35:45+00:00

Returning an array (Warning: Function returns address of local variable) ? interface int* decimalConversion(int

  • 0

Returning an array (Warning: Function returns address of local variable) ?

interface

int* decimalConversion(int iX);

implementation

int* decimalConversion(int iX){
    int iMult[10] = {0,0,0,0,0,0,0};

    ...

    return iMult;  // <-- Warning: Function returns address of local 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-23T23:35:47+00:00Added an answer on May 23, 2026 at 11:35 pm

    You should allocate space for the array. You’re returning the address of an array that was created on the stack (hence local variable warning) if you’re using C in that function use malloc(my_arr_size) if not use Objective-C’s alloc.

    Example:

    int *my_arr = calloc(10, sizeof(int)); // Make sure we get zeroed memory
    // Fill array
    return my_arr; // Should no longer give a warning
    

    When done with it just use free(my_arr) same as release. Reason I did this in C is because I can see that you’re returning an int* type and using C style declarations so if you’re doing it in Objective-C let me know, and I can change my answer’s example.

    The reason you are getting this error is because local arrays get put on the stack, when you return that array you return an address in a stack frame. The problem is that when that method finishes execution that stack frame is no longer valid and therefore you cannot expect any data that was on that frame to be valid (although there are cases when this does work, but it is considered bad practice). By allocating that array on the heap you can return a heap address where your data is assured to exist until you call free() on the pointer to that data.

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

Sidebar

Related Questions

im having trouble returning an array that contains decimal values to a variable: function
A SDK is returning me an array with multiple dimensions such as: int[,,] theArray
I have a PHP function that returns an array. This is the output of
Which way is good for returning an array of classes? class BigSizeClass { int
I've a web-service function which is returning an array of items to a PHP-Client.
I have my function getAllData, which is returning an array with dictionaries. - (NSArray
I have problems with marshalling output parameter of c++ function returning array of data
My var_dump is returning this: array(5) { [radioinput]=> string(12) sidebar-left [option1]=> int(0) [sometext]=> string(0)
I have some code where I'm returning an array of objects. Here's a simplified
In my rails app the model is fetching some XML and returning an array.

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.