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

  • Home
  • SEARCH
  • 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 7441813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:04:36+00:00 2026-05-29T11:04:36+00:00

In a C function, its locals’s memory is allocated when the function is called,

  • 0

In a C function, its locals’s memory is allocated when the function is called, and deallocated when function is finished. What about for functions that return a value (e.g. int, string), when and where does the return address’s memory is allocated and deallocated, and is it part of the call stack or the callee stack, or something else?

Consider the following example:

int* foo()
{
   int _myInt;
   return(&_myInt);
}

This example gets me completely confused as of how memory is allocated for the return address that return a pointer. Can someone please explain?

Same for C and Objective-C?

  • 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-29T11:04:37+00:00Added an answer on May 29, 2026 at 11:04 am

    I don’t know about Objective-C but, with standard C, return values are typically stored in registers.

    When the function returns a pointer type, it is up to the developer to ensure the memory pointed to remains valid after the call.

    Consider the following function:

    char* GetStr()
    {
        char buff[50];
        strcpy(buff, "Hello, World!");
        return buff;
    }
    

    This function returns a pointer to local memory, which is deallocated when the function returns. Accessing that memory would constitute a bug with undefined behavior.

    This also applies to your example, which is not valid. But since an int can fit in a register, you can simply do return _myInt; and return the value directly.

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

Sidebar

Related Questions

I have a thread that, when its function exits its loop (the exit is
I want to have a function in a different module, that when called, has
locals is a built in function that returns a dictionary of local values. The
The documentation for the locals() function specifically warns not to modify its output, as
I'm trying to keep a hash local to one function that remembers its state
Basically I have a application that sends a value to another function(located in another
(function() {})() and its jQuery-specific cousin (function($) {})(jQuery) pop up all the time in
Joomla has a built-in function on its login screen I've forgotten my user name,
I understand overriding a method/function redefines its implementation in the derived class from its
Is there any way how to set std::setw manipulator (or its function width )

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.