Passing on ostringstream

Question

Hi,

My c program looks likes this

void api1() {
cin << info;
ostringstream ostr;
ostr << info;
api2(ostr.str().c_str()); // Getting string from ostreamstream and
getting char* from string
}

void api2(const char* ch) {
insert map(ch);
}

Sample input to api2 "a" after that when i checked the content of map
at the end. I am seeing some junk values.

Can some one explain what is going wrong in my program? Do i should
not do c_str() on ostringstream?.

Thanks,
Suresh

Solution
vs********@gmail.com writes:
Hi,

My c program looks likes this

void api1() {
cin << info;
ostringstream ostr;
ostr << info;
api2(ostr.str().c_str()); // Getting string from ostreamstream and
getting char* from string
}

void api2(const char* ch) {
insert map(ch);
}

Sample input to api2 "a" after that when i checked the content of map
at the end. I am seeing some junk values.

Can some one explain what is going wrong in my program? Do i should
not do c_str() on ostringstream?.
The return value from std::string.c_str() points to storage owned by the
std::string. Once the object goes out of scope, the pointer is no longer
valid. The std::string object returned from std::ostringstream.str() goes
out of scope at the next sequence point, the end of the function call, so if
your api2() function stashed away that pointer somewhere, that pointer will
no longer be valid once api2() returns, and the caller''s function call is
complete.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkiAeoQACgkQx9p3GYHlUOLSWACdHrTzPjHMD3 tGn8CvQm7lQjL7
eUwAniynmx9HMFdHI/dpM5P 1KE5dzV/
=D03
-----END PGP SIGNATURE-----


On 2008-07-18 12:58, vs********@gmail.com wrote:
Hi,

My c program looks likes this

void api1() {
cin << info;
ostringstream ostr;
ostr << info;
api2(ostr.str().c_str()); // Getting string from ostreamstream and
getting char* from string
}

void api2(const char* ch) {
insert map(ch);
^^^^^^^^^^^^^^^^^ Syntax Error!
Sample input to api2 "a" after that when i checked the content of map
at the end. I am seeing some junk values.

Can some one explain what is going wrong in my program? Do i should
not do c_str() on ostringstream?.

I can not say for sure since I do not know what map is or how you insert
into it (please see section 5.8 in the FAQ). I would guess that you
insert ch in the map, and since ch is a pointer to a temporary (or at
least an object which will be destroyed at the end of api1()) you only
get garbage later on.

Instead of using c_str() to get a cont char* you should use the return-
value from str() as the key in the map, playing around with char arrays
unless you have to is bad.

--
Erik Wikstr??m

Hi Sam/Erick,

I am writing my program in detail

std::map<string,intcache;

void api1() {

cin << key; // string
cin << value; // int
ostringstream ostr;
ostr << key;
api2(ostr.str().c_str(), value);

}

void api2(const char* info, int value) {
cache[info] = value;
}

So, here i am not storing the info* but copying the value of that
pointer to string and keeping it in my MAP.
But when i print this cache it refers to junk value.

suresh