473,795 Members | 2,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1546
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
5044
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
7465
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
3871
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 things like #include <strstream.h> or
3
5610
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
1636
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
2803
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
3093
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 before rowID answID qryrow questionID datafield 1591 12 06e 06e 06e question 1593 12 06f 06f 06f question 1594 12 answer to the question 06f
6
4982
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 related to 'ostrstream' and the fact that it is being deprecated from the language, etc. What is the best way to handle fixing this replicating error? TIA, Eric
1
2380
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 writes error messages to this stream using the “<<” operator. 4) The EXE file creates an ostrstream variable and passes this to the DLL function. 5) After the DLL function has finished, the EXE writes some text to an output file, followed by the...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.