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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:02:13+00:00 2026-06-02T21:02:13+00:00

Why does the value of text1 change after reading out y in this piece

  • 0

Why does the value of text1 change after reading out y in this piece of code?

void func()
{
    int value1 = 5;
    double value2 = 1.5;
    std::ostringstream x, y;

    x << value1;
    y << value2;


    const char *text1 = x.str().c_str();
    fprintf(stderr, "text1: v=%s, p=%p\n", text1, &text1);
    const char *text2 = y.str().c_str();
    fprintf(stderr, "text1: v=%s, p=%p\ntext2: v=%s, p=%p\n", text1, &text1, text2, &text2);
}

Output:

text1: v = 5, a = 0xbfcfd508

text1: v = 1.5, a = 0xbfcfd508

text2: v = 1.5, a = 0xbfcfd510

  • 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-02T21:02:13+00:00Added an answer on June 2, 2026 at 9:02 pm

    After you have evaluated the str().c_str() expression, the temporary std::string instance created by the call to str() is released and your char pointer points into Nirvana. You need to store the return value of str()!

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

Sidebar

Related Questions

Does setting this value have the same effect as setting the debug=true in the
Does the timezone value change throughout the year to adjust to DST? So if
I have the code below. I'm wondering that does the self.value and _value have
I am having a hard time understanding the following issue: This code does what
Does anybody know if there is a way to display the text value of
What value does the hashCode() method return in java? I read that it is
I want to know what does RETURN_VALUE mean! I'm stuck at this thing. How
What is a scalable algorithm to print an N-binary-digit integer manually whose value does
Does anyone know how to convert the string value type -4,5 or 5,4 into
Why does the following simplified code not sets the font-size of the TextBlock to

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.