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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:30:47+00:00 2026-06-13T09:30:47+00:00

Who knows why the output is such? Although it’s wrong to use a pointer

  • 0

Who knows why the output is such?
Although it’s wrong to use a pointer like that, I would still like to understand why it behaves the way it does.

int* foo()
{
    int a=9;
    int *p=&a;
    //definitely not right to return a pointer to an invalid varible
    return p;
}
int main(int argc, char* argv[])
{
    int *p=foo();
    cout<<*p<<endl;//9
    cout<<*p<<endl;//2357228
    *p=2;
    cout<<*p<<endl;//2
    (*p)++;
    cout<<*p<<endl;//2357229
    cout<<*p<<endl;//2357228
    cout<<*p<<endl;//2357228
    (*p)++;
    cout<<*p<<endl;//2357229
    cout<<*p<<endl;//2357228

    return 0;

}
  • 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-13T09:30:48+00:00Added an answer on June 13, 2026 at 9:30 am

    Although it’s wrong to use a pointer like that, I would still like to
    understand why it behaves the way it does.

    Because p is left pointing at a location in stack memory that keeps getting written over by cout’s << method. Every time you use cout, the value of p may change.

    Code like this is dangerous as it can corrupt the stack and cause your program to crash.

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

Sidebar

Related Questions

Is there anyone out there who knows if it's possible to use common web
I am trying to look a file that looks who's path looks like this.
The output of sql commands that is visible to users who interactively run SQL
I have this table called logs that logs a who input or output data.
Who knows how to filter the Store right? I tried to do it in
Who knows a good component for a calendar control (NOT date/time picker)? Calendar control
Who knows good graph layout engines with clear customization and managment? Flare is an
Anyone who knows a good way to generate/create a barcode from a String in
Anyone out there who knows what are the differences between BasicRenderEngine and LazyRenderEngine?
Is there anyone who knows this? I have been trying this for the last

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.