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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:50:15+00:00 2026-06-10T06:50:15+00:00

Suppose i define the following code: int *func() { int *p=(int *)malloc(sizeof(int)); // memory

  • 0

Suppose i define the following code:

int *func()
{
    int *p=(int *)malloc(sizeof(int)); // memory is allocated from heap
                                      // which can be accessed anytime.

    return p;    // but we created p which has a local scope and vanishes as
                 //soon as function exits.  
}

Then how does this thing work? p being a local varible (a pointer holding a address to
the dynamic memory). I mean the memory itself from HEAP should definitely continue to exist, but the pointer variable has a local scope. How come i will be able to get this pointer?

  • 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-10T06:50:16+00:00Added an answer on June 10, 2026 at 6:50 am

    p is simply a pointer which refers to the memory block that you’ve allocated.

    The lifetime of the allocated block extends beyond the end of the function.

    It’s true that the actual pointer will go out of scope on return but you’ve already returned a copy of it it to the caller before then.

    And, as an aside, you probably meant int* (in both cases) rather than *int and (*int). In any event, you don’t want to explicitly cast the return value from malloc in C – it can hide subtle errors. C is perfectly capable of converting the void * returned by malloc into any suitable pointer type.

    It would be better written as something like:

    int *func (void) {
        int *p = malloc (sizeof (int));
        return p;
    }
    

    You’ll also notice the use of void in the function to explicitly state the function takes no parameters. That’s preferable to just having () which is subtly different (an indeterminate number of parameters).

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

Sidebar

Related Questions

I have issues understanding scopes in javascript.Let's suppose i have the following code: Ext.define('MA.controller.user',{
Suppose you have the following code: int main(int argc, char** argv) { Foo f;
Suppose you have the following code template<typename T,void (T::*m)(int)> struct B{ void f(T* a,int
Suppose you have the following code namespace a{ struct S{}; //void f(int){} } namespace
Suppose I have the following preprocessor definition #define MYNUMBER 10f; I would then like
Suppose I define a union like this: #include <stdio.h> int main() { union u
Suppose that I define some class: class Pixel { public: Pixel(){ x=0; y=0;}; int
Suppose I have the following files: lib/A.h #ifndef A_H #define A_H #include <vector> class
How does the following code compile correctly, #include <stdio.h> #define stringer( x ) printf_s(
I copied the following C code from K&R. The code is supposed to print

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.