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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:49:23+00:00 2026-06-10T19:49:23+00:00

Possible Duplicate: Can a local variable's memory be accessed outside its scope? What’s the

  • 0

Possible Duplicate:
Can a local variable's memory be accessed outside its scope?

What’s the problem with the second printf?

#include<stdio.h>
int* fun() {
   int a =10;
   return &a;
}
int main() {
   int *a;
   a = fun();
   printf("%d",*a);
   printf("%d",*a);
   return 0;
}

I have returned the address of local variable and passed it to the printf. The first time it prints correctly as “10”, but the second time it shows a junk value.

If initially a was a dangling pointer pointing to address of 10, why doesn’t it the second time?

Can anyone explain this?

I even tried calling some other function before calling printf the first time but I still get the same output.

After BeniBela’s ans i tried wit this..

#include<stdio.h>
int* fun()
{
int a =10;
return &a;
}
void fun2(int d)
{
int a,b,c;
}

int main()
{
int *a,b;
a = fun();
fun2(5);
printf("%d",*a);
printf("%d",*a);
return 0;
}

still same output..:(

  • 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-10T19:49:25+00:00Added an answer on June 10, 2026 at 7:49 pm

    What actually happens is:

    All local variables are stored on the stack.

    1. Before the call to fun, the stack contains only the *a variable of main, like: |int *a = undefined||

    2. When fun is called, the parameters to fun (i.e. none), the address to main and the local variables of fun are added to the stack: |int *a = undefined| return to main | int a = 10 || (there is also the frame pointer but that doesn’t matter)

    3. After fun returns, the stack is |int *a = 2nd next stack var|| return to main | int a = 10 |. The last 2 stack variables are invalid, but they are still there.

    4. When the first printf is called the parameters to printf (in inverse order *a then” %d”), the return address and the local variables of printf are added again after *a and override the old values:
      It first becomes |int *a = 2nd next stack var| int a = 10 || int
      a = 10 |

      then |int *a = 2nd next stack var| int a = 10 | "%d" |
      then |int *a = 2nd next stack var| int a = 10 | "%d" | return to
      main ||

      and finally |int *a = 2nd next stack var| int a = 10 |
      "%d" | return to main | local vars of printf ||

    5. When the second printf is called, *a still points to the second bin, but that one contains now “%d”, which will appear as a strange number

    [edit:]

    The fun2 does not override the 10 on the stack, because gcc reserves empty bins on the stack, where the arguments of called functions are put. So it is not |int *a = 2nd next stack var| return to main | int a = 10 | as I wrote above, but more like |int *a = 4th next stack var | empty | empty | return to main | int a = 10 |.
    When fun2 is called, it becomes |int *a = 4th next stack var | empty | 5 | return to main | int a = 10 |, and the 10 is still not overriden.

    The int *a,b,c within the function do not matter, since they do not have a value assigned.

    With gdb you can look at the actual stack (it grows backward):

    Before fun:

    0xffffd454:    0x080496f0    0xffffd488    0x080484eb    0x00000001
                     noise     (framepointer)  address      noise/empty bin     
                                               in main
    

    After fun, before fun2 and its arguments:

    0xffffd454:    0x0000000a    0xffffd488    0x08048441    0x00000001
                     a         (framepointer)   address     noise/empty bin     
                                                in main
    

    After fun2:

    0xffffd454:    0x0000000a    0xffffd488    0x08048451    0x00000005
    
                     a         (framepointer)   address       argument     
                                                in main       for fun2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can a local variable's memory be accessed outside its scope? #include <iostream>
Possible Duplicate: Can a local variable's memory be accessed outside its scope? input: #include
Possible Duplicate: Can a local variable's memory be accessed outside its scope? today i
Possible Duplicate: Can a local variable's memory be accessed outside its scope? I saw
Possible Duplicate: Can a local variable's memory be accessed outside its scope? I'm trying
Possible Duplicate: Can a local variable's memory be accessed outside its scope? Is there
Possible Duplicate: Can a local variable's memory be accessed outside its scope? I thought
Possible Duplicate: Can a local variable's memory be accessed outside its scope? When Automatic
Possible Duplicate: Can a local variable's memory be accessed outside its scope? return reference
Possible Duplicate: Pointer to local variable Can a local variable's memory be accessed outside

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.