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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:34:39+00:00 2026-06-04T01:34:39+00:00

I’m having some issues with std::cout, std::stringstream, and std::string.c_str(). Mainly, it seems that there’s

  • 0

I’m having some issues with std::cout, std::stringstream, and std::string.c_str(). Mainly, it seems that there’s stuff that’s getting caught in a buffer somewhere, and I’m not sure how to fix the problem.

If you don’t like reading code in StackOverflow, here are the relevant links to my github:
The TLString class, the Test class, and the unit test — and you can skip to the end where I state my more concise question.

In my unit test, I have the following code:

    Test <std::string> strtest; // A unit test object expecting strings.
    Test <const char*> chtest; // A unit test object expecting const char*s
    // ...

    TurnLeft::Utils::TLString str3("Exterminate.");
    // ...

    /* Basically, name() and expect() will use the passed arg. 
     * in the output in order to
     * display output such as the following:
     * str(): expecting 'Exterminate.' | Actual 'Exterminate.' => PASS
     */
    strtest.name("str()").expect("Exterminate.").test( str3.str() );

    /* To try and determine where the corruption was occuring, I did a 
     * simple cout here, and got what should be the expected result of 
     * the next test,
     * meaning that the actual test should be succeeding.
     */
    std::cout << str3.c_str() << std::endl //outputs Exterminate. normally.

    /* But when I try to invoke that same method (c_str()) within the test
     * object, it simply copies the argument passed in for name().
     */
    chtest.name("c_str()").expect("Exterminate.").test( str3.c_str() );
    // Should output 'Exterminate.' as in the saatement before, but instead
    // outputs 'c_str()'.

Here’s the code for the Test class:

namespace unittest{
static std::string status[2] = {"FAIL", "PASS"};

    template <class ExpectedResult>
    class Test
    {
     private:
        ExpectedResult  expected;
        ExpectedResult  actual;
        std::string     testName;
     public:
        Test();
        Test <ExpectedResult>& expect (ExpectedResult value);
        Test <ExpectedResult>& name   (std::string);
        void test   (ExpectedResult value);
    };

template <class ExpectedResult> Test <ExpectedResult>&
Test<ExpectedResult>::expect(ExpectedResult value)
{    
    expected = value;
    return *this;
}

template <class ExpectedResult>
Test <ExpectedResult>&
Test<ExpectedResult>::name(std::string aName)
{
    testName = aName;
    return *this;
}

    template <class ExpectedResult>
    void Test<ExpectedResult>::test(ExpectedResult value)
    {
        actual = value;
        std::cout << testName << ": ";
        std::cout << "Expecting: " << expected << " | ";
        std::cout << "Actual: " << actual;
        std::cout << " => " << status[actual==expected] << std::endl;
    }

The TLString class is one that I’m writing that will give some more fluid operation to strings in C++ (for example, concatenation). It uses a stringstream in order to handle these operations. The TLStream::c_str() method is really just doing this: return stream.str().c_str();

So, I’m really confused about how actual is being assigned the value of testName. I’m not sure where the conflict is occurring, given that the only time those to variables come close to interacting is when they are both output to to the CLI, and even moreso as in this case, those two are different datatypes.

I’ve written in the c_str() functionality because, quite simply, you just never know when some third-party library is going to rely on C strings instead of C++ strings, and saw no reason to limit my class. Even within std::ios, you need to use c strings for some things.

Any help would be greatly appreciated.

Thanks!

  • 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-04T01:34:40+00:00Added an answer on June 4, 2026 at 1:34 am

    std::stringstream.str() returns a temporary object of type std::string. This temporary goes out of scope when TLStream::c_str() returns, leaving the returned char const* pointer pointing to freed memory.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
Specifically, suppose I start with the string string =hello \'i am \' me And

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.