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

When is the std::string dstor called in the following code

Hi C++ gurus,

Why in the following code the std::string destructor is being called
before the 'catch' ?

Thanks,
-Mathieu

#include <iostream>
#include <sstream>

void foo()
{
std::ostringstream os;
os << "my error";
throw os.str().c_str();
}

int main()
{
try
{
foo();
}
catch(const char *msg)
{
std::cerr << msg << std::endl;
}
return 0;
}
Feb 12 '08 #1
1 1408
On Feb 12, 12:09 am, mathieu <mathieu.malate...@gmail.comwrote:
Hi C++ gurus,

Why in the following code the std::string destructor is being called
void foo()
{
.....
throw os.str().c_str();

os.str() creates a std::string object and returns it as a result. As
you are using it in an expression it is a temporary object and thus
will be destroyed at the end of the statement.

The value returned by c_str() is not valid beyond the lifetime of the
object it was called on. So your best bet to actually throw the
std::string object.
int main()
{
try
{
foo();
}
Add
catch(std::string const& e)
{
std::cerr << e << std::endl;
}

catch(const char *msg)
{
std::cerr << msg << std::endl;
}
return 0;

}
Feb 12 '08 #2

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

Similar topics

13
by: Glen Able | last post by:
Obviously the following doesn't work: int i = 5; std::string myString = "Number is " + i + " thankyou please"; So can anyone give me some idea what's the nicest way to this sort...
1
by: Tero Toivanen | last post by:
Dear experts, I am doing code to Solaris 9 system with C++. I get every now and then segmentation fault in the following code that removes heading and trailing white spaces (mLineStr is of...
3
by: Heiko Hund | last post by:
Hi, I do not understand the deeper reason for the following compiler error $ g++ test.cpp test.cpp: In function `int main()': test.cpp:41: error: `std::basic_string<char,...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
11
by: Fred | last post by:
Hi I'm new to the std library. I have a function which reads data and creates a std string from it. I want to pass that back to the calling function as a parameter, not as the function return. ...
5
by: sean345 | last post by:
While compiling my program I get the following g++ error: error: no matching function for call to `ErrorLog::file_error(std::string&, std::string&, const char)' note: candidates are: void...
2
by: HerbD | last post by:
I have a loooong debugging session behind me! I finally found the reason for the problem and now would like to know, if it is a bug in my code or not standardconformant behavour of the compiler(s) or...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.