473,320 Members | 1,900 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,320 software developers and data experts.

ostrstream question

I encountered the following code similar to this
// some header files
static char* func(int i)
{
ostrstream out;

if (i==1) out << "ABCDE";
else if (i==2) out << "123";
else cout << "";

return out.str();
}

int main ()
{
static char *p1 = NULL, *p2 = NULL;
p1 = func(1);
p2 = func(2);
printf ("String1 = %s, String2 = %s\n", p1, p2);
return 0;
}
The desired out put is "String1 = ABCDE, String2 = 123".
New if I used an character array, char out[10], and returning &out[0]
instead of ostrstream this would definitly be illegal.
But this looks illegal too, I suppose ostrstream has some destructor
that deletes any allocated memory. What is confusing is that this
happens to work when i try it. Is this legal after all?
Jul 22 '05 #1
1 1515
becte wrote:
I encountered the following code similar to this
// some header files
static char* func(int i)
{
ostrstream out;

if (i==1) out << "ABCDE";
else if (i==2) out << "123";
else cout << "";

return out.str();
You're returning a property of an object about to be destroyed.
}

int main ()
{
static char *p1 = NULL, *p2 = NULL;
p1 = func(1);
p1 here is invalid because it contains a pointer value to a buffer of
a stream that is no more.
p2 = func(2);
Same problem here. p2 is invalid.
printf ("String1 = %s, String2 = %s\n", p1, p2);
return 0;
}
The desired out put is "String1 = ABCDE, String2 = 123".
New if I used an character array, char out[10], and returning &out[0]
instead of ostrstream this would definitly be illegal.
Right.
But this looks illegal too, I suppose ostrstream has some destructor
that deletes any allocated memory. What is confusing is that this
happens to work when i try it. Is this legal after all?


No.

V
Jul 22 '05 #2

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

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
9
by: Charles Prince | last post by:
How do I reuse a ostrstream? So far I have replaced all code that does this "delete <ostrstream>.str()" with "<ostrstream>.freeze(0)"
2
by: b83503104 | last post by:
Hi, An old code is using stuff like #include <strstream.h> ostrstream *m_actionStream; and the compiler complained syntax error before `*' token I followed some previous posts and tried...
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;
2
by: Pep | last post by:
I have inherited a program that does this ostrstream os; os << "some text"; os.str() = '\0'; In fact is there any point doing it at all? TIA, Pep.
1
by: tcl | last post by:
Questions on ostrstream. #1) do I have a memory leak as the control exits the scope { ostrstream os; os << "hello world" << endl << ends; }
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
6
by: Eric Chomko | last post by:
I inherited a task which uses a lot of 'ostrstream' in the code. The port is from an SGI box to Sun which is using GNU C++ (g++ version 3.4.2). When trying to compile I get several errors all...
1
by: GeeBee | last post by:
1) I’m using Borland C++ Builder 1.0 (a very old (but still good) version). 2) I have an application EXE calling a DLL. 3) A function in the DLL receives a parameter defined as "ostream &" , and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.