473,396 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing on ostringstream

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
Jul 18 '08 #1
13 2426
Sam
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-----

Jul 18 '08 #2
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
Jul 18 '08 #3
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
Jul 18 '08 #4
vs********@gmail.com writes:
I am writing my program in detail

std::map<string,intcache;

void api1() {

cin << key; // string
cin << value; // int
Do you really have << operators on cin?
ostringstream ostr;
ostr << key;
api2(ostr.str().c_str(), value);

}

void api2(const char* info, int value) {
cache[info] = value;
}
--
Ben.
Jul 18 '08 #5
On Jul 18, 6:19*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
vsuresh...@gmail.com writes:
I am writing my program in detail
std::map<string,intcache;
void api1() {
* cin << key; // string
* cin << value; // int

Do you really have << operators on cin?
* ostringstream ostr;
* ostr << key;
* api2(ostr.str().c_str(), value);
}
void api2(const char* info, int value) {
* cache[info] = value;
}

--
Ben.
Sorry Ben,

its cin >key;
cin >value;

+suresh
Jul 18 '08 #6
On 2008-07-18 14:51, vs********@gmail.com wrote:
Hi Sam/Erick,

I am writing my program in detail
No you are not, please post a minimal but compilable and functional
program that demonstrates your problem.
std::map<string,intcache;

void api1() {

cin << key; // string
cin << value; // int
ostringstream ostr;
ostr << key;
This does not make sense, if key is a string why use a stringstream to
convert it to a string?
api2(ostr.str().c_str(), value);

}

void api2(const char* info, int value) {
cache[info] = value;
}
Same here, since cache is of type map<string, intwhy are you messing
around with char*? Just pas key as a string if that is what you want.

--
Erik Wikström
Jul 18 '08 #7
vs********@gmail.com wrote:
Sorry Ben,

its cin >key;
cin >value;
so far, your biggest mistake is not to write a very small program that
compiles and that presents the error. For me, your mistake can be
anywhere else in parts of the program that are not shown.

Best wishes,

Zeppe
Jul 18 '08 #8
Hi Zeppe,

I agree i did not want to create a stand alone program that gives the
error.

In simple words, is it advisable to pass ostringstream as a char* to
another interface ?

Thanks,
Suresh
Jul 19 '08 #9
vs********@gmail.com wrote:
Hi Zeppe,

I agree i did not want to create a stand alone program that gives the
error.
Then how do you expect people to help?
In simple words, is it advisable to pass ostringstream as a char* to
another interface ?
No.

--
Ian Collins.
Jul 19 '08 #10
Then how do you expect people to help?
Hi Ian,

my subject is "passing on ostringstream" and its not related to cin/
cout or syntax error. But people are pointing to the syntax error and
other optimizations that can be done on my program. As i am new to
this group, i dont know that i want to write 100% error free code :
( will writing sample pgms.

will be clearer from next post.
>
In simple words, is it advisable to pass ostringstream as a char* to
another interface ?

No.
Can i know the reason why its not advisable ?

+suresh
Jul 19 '08 #11
vs********@gmail.com wrote:
>Then how do you expect people to help?
Hi Ian,

my subject is "passing on ostringstream" and its not related to cin/
cout or syntax error. But people are pointing to the syntax error and
other optimizations that can be done on my program.
Part of the problem is the question was not clear. You are attempting
to pass the value of the contents of the stream as a const char* rather
than passing on the stream object.
>>In simple words, is it advisable to pass ostringstream as a char* to
another interface ?
>No.

Can i know the reason why its not advisable ?
As others have pointed out, you were passing a pointer to a temporary
object which will be destroyed when it goes out of scope. Erick also
pointed out the solution.

--
Ian Collins.
Jul 19 '08 #12
On 2008-07-19 11:34, vs********@gmail.com wrote:
>Then how do you expect people to help?
Hi Ian,

my subject is "passing on ostringstream" and its not related to cin/
cout or syntax error.
But none of your problems or questions were about passing an ostringstream.
But people are pointing to the syntax error and
other optimizations that can be done on my program.
I have not seen any advice about optimisations, but I have seen (and
given) some about good design and writing good code. While it is
possible to write error-free code in a bad way, it is much easier to
reduce the errors if the code quality is high.
As i am new to
this group, i dont know that i want to write 100% error free code :
( will writing sample pgms.
Since you are new to C++ and it is a complex language it might be hard
for you to figure out what the errors are, and (again since it is a
complex language) it might be hard for us too if we can not see all the
code. We do not require you to post 100% error free code, but we will
point out the errors we find, in fact we do not even require that you
post compiling code in some instances, but we do want you to post the
actual code you are having problems with.
In simple words, is it advisable to pass ostringstream as a char* to
another interface ?

No.

Can i know the reason why its not advisable ?
It is never advisable to us char* when you want strings unless you
really have to, use std::string instead whenever possible. Especially as
in your case what you really want is a string (since that is what you
have in you map) and not a char*.

--
Erik Wikström
Jul 19 '08 #13
Thanks Erik

Sounds good.
Jul 20 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927...
6
by: Eric Boutin | last post by:
Hi ! I have a strange problem with a std::ostringstream.. code : #include <sstream> /*...*/ std::ostringstream ss(); ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\"...
5
by: Simon Pryor | last post by:
I am having some strange problems using std::ostringstream. The simple stuff works okay, but trying to use: std::ostringstream::str(const std::string&) or:...
2
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have...
3
by: Mathieu Malaterre | last post by:
Hello, I am trying to write this simple code: std::ostringstream s; s << 1024; std::cout << s.str() << std::endl; s.str(""); // <- problem s << 512; std::cout << s.str() << std::endl;
3
by: Kyle Kolander | last post by:
I posted this in response to a previous thread but have not gotten any replies back... Hopefully someone has an answer? From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears...
3
by: Bob Altman | last post by:
Hi all, Why doesn't the following unmanaged C++ code work as expected: string s; ostringstream strm(s); // This stream should store results in s strm << 25; cout << s << endl; // s...
3
by: Generic Usenet Account | last post by:
With the deprecated ostrstream class, when the constructor is invoked without arguments, memory is dynamically allocated. In that case the onus on freeing the memory lies with the user. Typically...
11
by: coomberjones | last post by:
I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.