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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:55:18+00:00 2026-05-25T23:55:18+00:00

Below is the code The Code: #include <stdio.h> int * num(void); int main(void) {

  • 0

Below is the code

The Code:

#include <stdio.h>

int * num(void);

int main(void)
{
    int * num2;
    num2 =num();
    printf("%d\n" , *num2);

    return 0;
}


int * num(void)
{
    int num = 20;

    return &num;
}

The Question :

  1. As we know , the function num is local to its function num() , so in this code I try to return the address of the variable num in the function to the function that calls it , which is main().

  2. After that I just use the dereferencing operator to extract the value of the specific num variable and print it out in main() function.

  3. There’s one thing i’m confused . I remember i read a book about javascript that mention a variable lifetime is within the function , which mean after the function finish performing its instructions and pass the control back to the function that calls it , the value of each variable in the function will be clean out(garbage collector).But why in this code my main() function still can point to the value of that specific memory address??

  • 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-25T23:55:19+00:00Added an answer on May 25, 2026 at 11:55 pm

    The reason you can see the value of the variable is because of how the stack works. Effectively as you enter the function num, a pointer (the stack pointer) is moved to add space in for the local storage of the function. When you exit the function the stack pointer is moved back effectively meaning that the next function call will overwrite the stack storage used in the previous function call. Until it is overwritten, however, the value exists in a sort of limbo. It’s still actually there in memory but may get overwritten at any moment. The existence of the actual value there may or may not be the case. That is why doing as you do above is known as undefined behaviour.

    Basically … don’t do it.

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

Sidebar

Related Questions

Below is a code which I am not understand. #include<stdio.h> int main(int argc, char
Below is my code (test.c): #include <stdio.h> #include <string.h> int main(int argc, char **argv)
i have following code #include <stdlib.h> #include <stdio.h> int sorter( const void *first_arg,const void*
How would I achieve the pseudo-code below in JavaScript? I want to include the
I would like to know how to modify the below code to strip =20
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
I'd like to copy and call a function, but the code below segfaults when
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
When running the below code a type is never returned, despite there being a
In the below code snippet can i replace char * to const char *

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.