0

I have an issue using stringstreams

When I run this code the first printf is fine but at some point it gets contaminated and prints out a shorter string.

string CJpsURI::getURIwithSipIpPort() { stringstream ss; ss << "sip:" << m_ipAddr << ":" << m_portAddr; string out = ss.str(); } main() { CJpsURI localIp(); localIp.setIpPort("192.168.88.1", 5060); char *s = localIp.getURIwithSipIpPort().c_str(); printf("This is the sip port: %s", s); // this may be ok -- sip:192.168.88.1:5060 // do some stuff printf("This is the sip port: %s", s); // wrong; -- sip:192.168.8/030/004 } 

It almost appears that the *s is pointing to the out string on the stack which gets destroyed. But that should not be happening since I am returning out and not a reference to out.

But this seems to work.

string CJpsURI::getURIwithSipIpPort() { string out = (boost::format("sip:%1%:%2%") % m_ipAddr % m_portAddr).str(); return out; } main() { CJpsURI localIp(); localIp.setIpPort("192.168.1.1", 5060); char *s = localIp.getURIwithSipIpPort().c_str(); printf("This is the sip port: %s", s); // this may be ok // do some stuff printf("This is the sip port: %s", s); // this will be ok too; } 

Any ideas would be appreciated

4
  • 7
    Your first function does not return anything. Commented Nov 17, 2014 at 20:33
  • 3
    Which is undefined behavior (in reference to @Galik's comment.) Commented Nov 17, 2014 at 20:35
  • 1
    Even if you do return out, s is the result of calling c_str() on a temporary string, so dereferencing it is undefined. In both cases. It's just bad luck that it "works". Commented Nov 17, 2014 at 20:36
  • So many comments in my code trying various thing that I did a bad copy and paste. My original code had "return out" as my last statement. Commented Nov 17, 2014 at 22:08

3 Answers 3

3

You have two problems:

  1. As remyabel and Galik pointed out: Your function does not return. So I would Concur with Galik that you need to adapt your function to:

string CJpsURI::getURIwithSipIpPort()
{
stringstream ss;
ss << "sip:" << m_ipAddr << ":" << m_portAddr;
return ss.str();
}

  1. A char* points to an array of chars. Here you want the array found inside the string returned by getURIwithSipIpPort. But that memory will be released as soon as this line ends! There is nothing hanging on to it. So what you really need to do is:

    string s{localIp.getURIwithSipIpPort()};

Sign up to request clarification or add additional context in comments.

2 Comments

So many comments in my code trying various thing that I did a bad copy and paste. My original code had "return out" as my last statement.
@user846566 If setting the code up as explained in one of our answers solved your problem I'd encourage you to accept that answer.
2

The C++ standard says:

§ 6.7/2 [..] Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

The second problematic statement is:

char *s = localIp.getURIwithSipIpPort().c_str(); 

First, the conversion from const char* to char* is deprecated. Use const char*. Second, don't do this in the first place. If you want to prolong the lifetime of a temporary, use a const reference.

const std::string& s = localIp.getURIwithSipIpPort().c_str(); 

Comments

1

Your first function does not return anything:

string CJpsURI::getURIwithSipIpPort() { stringstream ss; ss << "sip:" << m_ipAddr << ":" << m_portAddr; string out = ss.str(); // This does not get returned! } 

Try this:

string CJpsURI::getURIwithSipIpPort() { stringstream ss; ss << "sip:" << m_ipAddr << ":" << m_portAddr; return ss.str(); // Now it returns something! } 

1 Comment

So many comments in my code trying various thing that I did a bad copy and paste. My original code had "return out" as my last statement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.